常见事件及相应事件源类型
事件源
用户操作
事件
JButton
点击按钮
ActionEvent
JTextField
在文本域按回车键
ActionEvent
JCheckBox
点击复选框
ActionEvent, ItemEvent
JRadioButton
点击单选按钮
ActionEvent, ItemEvent
JComBox
选定选项
ActionEvent, ItemEvent
JList
选定选项
ListSelectionEvent
JMenuItem
选定菜单项
ActionEvent, ItemEvent
JSlider
滑动滑块
ChangeEvent
Window
窗口打开,关闭等
WindowEvent
Component
点击或移动鼠标
MouseEvent
Component
按下或释放键盘上的键
KeyEvent
Container
在容器中添加或删除组件
ContainerEvent
Component
组件获得或失去焦点
FocusEvent
Component
组件移动,改变大小
ComponentEvent
JScrollBar
移动滚动条
AdjustmentEvent
java事件处理机制使用的是事件委托处理模型.事件监听器并不会自动接收某个组件触发的事件,要想监听器接收某个组件触发的某种事件,
就必须在该事件源中注册它(因此,程序可以有选择的忽略事件).所谓注册就是调用事件源提供的注册方法来声明某个对象是该事件源的监听器.
java语言中每种类型的事件定义了相应的监听器接口和注册方法.
一般来说,XXX事件的监听器接口是XXXListener,注册方法是addXXXListener.
部分常见的监听器接口
事件
监听器接口及注册方法
监听器方法
ActionEvent
ActionListener
addActionListener
actionPerformed(ActionEvent e)
AdjustmentEvent
AdjustmentListener
addAdjustmentListener()
adjustValueChanged(AdjustmentEvent e)
ComponentEvent
ComponentListener
addComponentListener()
componentResized(ComponentEvent e)
componentMoved(ComponentEvent e)
componentShown(ComponentEvent e)
componentHidden(ComponentEvent e)
ContainerEvent
ContainerEventListener
addContainerEventListener()
componentAdded(ContainerEvent e)
componentRemoved(ContainerEvent e)
FocusEvent
FocusListener
addFocusListener()
focusGained(FocusEvent e)
focusLost(FocusEvent e)
ItemEvent
ItemListener
addItemListener()
itemStateChanged(ItemEvent e)
KeyEvent
KeyListener
addKeyListener()
keyTyped(KeyEvent e)
keyPressed(KeyEvent e)
keyReleased(KeyEvent e)
MouseEvent
MouseListener
addMouseListener()
mouseClicked(MouseEvent e)
mousePressed(MouseEvent e)
mouseReleased(MouseEvent e)
mouseEntered(MouseEvent e)
mouseExited(MouseEvent e)
MouseEvent
MouseMotionListener
MouseMotionListener()
mouseDragged(MouseEvent e)
mouseMoved(MouseEvent e)
TextEvent
TextListener
addTextListener()
textValueChanged(TextEvent e)
WindowEvent
WindowListener
addWindowListener()
stateChanged(ChangeEvent e)
ListSelectionEvent
ListSelectionListener
addListSelectionListener()
valueChanged(ListSelectionEvent e)
1 importjava.awt.event.ActionEvent;2 importjava.awt.event.ActionListener;3
4 importjavax.swing.JButton;5 importjavax.swing.JFrame;6
7
8 public class JButtonEvent extends JFrame implementsActionListener{9 publicJButtonEvent(){10 JButton button=new JButton("按钮");11 button.addActionListener(this); //注册12 add(button);13 }14 public static voidmain(String[] args) {15 JButtonEvent frame=newJButtonEvent();16 frame.setTitle("演示动作事件处理");17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);18 frame.setSize(500,300);19 frame.setVisible(true);20 }21 @Override22 public voidactionPerformed(ActionEvent e) { //实现监听器接口中的方法23 System.out.println(((JButton)e.getSource()).getText());24 }25 }
运行结果
这种编程方式有许多不足,如可读性,重用性不佳等.现在比较流行使用内部类,特别是匿名内部类来实现监听类接口.
1 importjava.awt.event.ActionEvent;2 importjava.awt.event.ActionListener;3
4 importjavax.swing.JButton;5 importjavax.swing.JFrame;6
7 public class JButtonEvent1 extendsJFrame {8 publicJButtonEvent1(){9 JButton button=new JButton("测试按钮");10 button.addActionListener(new ActionListener() {//匿名内部类
11
12 @Override13 public voidactionPerformed(ActionEvent e) {14 System.out.println(((JButton)e.getSource()).getText());15 }16 });17 add(button);18 }19 public static voidmain(String[] args) {20 JButtonEvent1 frame=newJButtonEvent1();21 frame.setTitle("演示动作事件处理");22 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);23 frame.setSize(500,300);24 frame.setVisible(true);25 }26 }