改变对象状态被称为事件。例如,点击按钮、拖动鼠标等。java.awt.event 包提供了许多事件类和监听器接口来进行事件处理。
Java事件类和监听器接口
事件类
监听器接口
ActionEvent
ActionListener
MouseEvent
MouseListener and MouseMotionListener
MouseWheelEvent
MouseWheelListener
KeyEvent
KeyListener
ItemEvent
ItemListener
TextEvent
TextListener
AdjustmentEvent
AdjustmentListener
WindowEvent
WindowListener
ComponentEvent
ComponentListener
ContainerEvent
ContainerListener
FocusEvent
FocusListener
执行事件处理的步骤
执行事件处理需要以下步骤:
将组件与监听器注册
注册方法
为了将组件与监听器注册,许多类提供了注册方法。例如:
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextLis