8.事件处理


loadButton.addActionListener(
<span style="white-space:pre">	</span>EventHandler.create(ActionListener.class, frame, "loadData"));



java.beans.EventHandler 1.4

• static Object create(Class listenerInterface, Object target, String
action)
• static Object create(Class listenerInterface, Object target, String
action, String eventProperty)
• static Object create(Class listenerInterface, Object target, String

action, String eventProperty, String listenerMethod)


改变观感

  1. jre/lib目录下的swing.properties              swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel
  2. UIManager.setLookAndFeel       SwingUtilities.updateComponentTreeUI

String plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
try
{
<span style="white-space:pre">	</span>UIManager.setLookAndFeel(plaf);
<span style="white-space:pre">	</span>SwingUtilities.updateComponentTreeUI(panel);
}
catch(Exception e) { e.printStackTrace(); }


UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();


String name = infos[i].getName();
String className = infos[i].getClassName();

外部对象的this指针必须将外部类名作为前缀

javax.swing.UIManager 1.2

javax.swing.UIManager.LookAndFeelInfo 1.2

适配器类

WindowListener listener = . . .;
frame.addWindowListener(listener);

public interface WindowListener
{
void windowOpened(WindowEvent e);
void windowClosing(WindowEvent e);
void windowClosed(WindowEvent e);
void windowIconified(WindowEvent e);
void windowDeiconified(WindowEvent e);
void windowActivated(WindowEvent e);
void windowDeactivated(WindowEvent e);
}


WindowAdapter是对WindowListener的空实现;


frame.addWindowListener(new WindowAdapter()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>public void windowClosing(WindowEvent e)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>if (user agrees)
<span style="white-space:pre">			</span>System.exit(0);
<span style="white-space:pre">		</span>}
});

java.awt.event.WindowStateListener 1.4
• void windowStateChanged(WindowEvent event)


java.awt.event.WindowEvent 1.1
• int getNewState() 1.4
• int getOldState() 1.4


动作

The Action interface has the following methods:    //扩展了ActionListener
void actionPerformed(ActionEvent event)
void setEnabled(boolean b)
boolean isEnabled()
void putValue(String key, Object value)
Object getValue(String key)
void addPropertyChangeListener(PropertyChangeListener listener)
void removePropertyChangeListener(PropertyChangeListener listener)


AbstractAction 实现了除actionPerformed外的所有方法




处理按键时,与focus 有关

每个JComponent有三个输入映射





/**
 * A frame with a panel that demonstrates color change actions.
 */
public class ActionFrame extends JFrame
{
   private JPanel buttonPanel;
   private static final int DEFAULT_WIDTH = 300;
   private static final int DEFAULT_HEIGHT = 200;

   public ActionFrame()
   {
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      buttonPanel = new JPanel();

      // define actions
      Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"),
            Color.YELLOW);
      Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
      Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);

      // add buttons for these actions
      buttonPanel.add(new JButton(yellowAction));
      buttonPanel.add(new JButton(blueAction));
      buttonPanel.add(new JButton(redAction));

      // add panel to frame
      add(buttonPanel);

      // associate the Y, B, and R keys with names
      InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
      imap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow");
      imap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
      imap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");

      // associate the names with actions
      ActionMap amap = buttonPanel.getActionMap();
      amap.put("panel.yellow", yellowAction);
      amap.put("panel.blue", blueAction);
      amap.put("panel.red", redAction);
   }
   
   public class ColorAction extends AbstractAction
   {
      /**
       * Constructs a color action.
       * @param name the name to show on the button
       * @param icon the icon to display on the button
       * @param c the background color
       */
      public ColorAction(String name, Icon icon, Color c)
      {
         putValue(Action.NAME, name);
         putValue(Action.SMALL_ICON, icon);
         putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());
         putValue("color", c);
      }

      public void actionPerformed(ActionEvent event)
      {
         Color c = (Color) getValue("color");
         buttonPanel.setBackground(c);
      }
   }
}


javax.swing.JComponent 1.2
• ActionMap getActionMap() 1.3
returns the map that associates action map keys (which can be arbitrary
objects) with Action objects.
• InputMap getInputMap(int flag)1.3
gets the input map that maps key strokes to action map keys.



鼠标事件


java.awt.event.InputEvent 1.1
• int getModifiersEx() 1.4
returns the extended or “down” modifiers for this event. Use the following
mask values to test the returned value:
BUTTON1_DOWN_MASK
BUTTON2_DOWN_MASK
BUTTON3_DOWN_MASK
SHIFT_DOWN_MASK
CTRL_DOWN_MASK
ALT_DOWN_MASK
ALT_GRAPH_DOWN_MASK
META_DOWN_MASK
• static String getModifiersExText(int modifiers) 1.4
returns a string such as "Shift+Button1" describing the extended or “down”
modifiers in the given flag set.




AWT事件继承层次

所有事件有 util 包中的EventObject类扩展而来





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值