8.事件处理
2015-07-05 20:56
211 查看
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)
UIManager.setLookAndFeel SwingUtilities.updateComponentTreeUI
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
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的空实现;
java.awt.event.WindowStateListener 1.4
• void windowStateChanged(WindowEvent event)
java.awt.event.WindowEvent 1.1
• int getNewState() 1.4
• int getOldState() 1.4
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有三个输入映射
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.
• 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.
<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)
改变观感
jre/lib目录下的swing.properties swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeelUIManager.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: //扩展了ActionListenervoid 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.b 4000 lue", 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类扩展而来相关文章推荐
- SQL Server 2008的数据库快照功能介绍
- Ganglia监控搭建
- ajax的全局事件
- ubuntu change passward
- leetCode 13.Roman to Integer (罗马数字转整形) 解题思路和方法
- SQL Server中的Merge关键字
- Hadoop运行原理详解
- mosquitto在Linux环境下的部署/安装/使用/测试
- STL之rb_tree的find函数
- DELL R710在阵列卡上做raid的图例
- mosquitto在Linux环境下的部署/安装/使用/测试
- Debian - 设置MYSQL开机启动
- 图形化界面知识点
- javascript 删除数组中所有指定的值
- 厦门海沧区磁盘只有1TB的解决方案
- 关于mac版eclipse的配置问题
- 简单dp HDU 2151
- debian终端默认编辑器
- hibernate它 11.many2many双向
- 反射知识总结