Download presentation
Presentation is loading. Please wait.
1
AWT Event Model
2
What is an Event? Event Event Source Event handler 产生事件的对象。
事件,就是发生在用户界面上的用户交互行为所产生的一种效果。 Event Source 产生事件的对象。 Event handler 接收事件对象并对其进行处理的方法。
3
事件处理模型 Hierachical model(JDK 1.0) 事件传递机制。
Delegation model(JDK 1.1, 1.2) 授权处理机制。
4
Delegation Model 将事件源对象和事件处理器(事件监听器)分开。
5
import java.awt.*; import java.awt.event.*; public class TestButton { public static void main(String args[]) { Frame f = new Frame("Test"); Button b = new Button("Press Me!"); b.addActionListener(new ButtonHandler()); f.setLayout(new FlowLayout()); f.add(b); f.setSize(200,100); f.setVisible(true); }
6
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) { System.out.println("Action occurred"); }
7
使用JDK1.1授权处理模型进行事件处理的一般方法:
对于某种类型的事件XXXEvent,要想接收并处理这类事件,必须定义相应的事件监听器类,该类需要实现针对特定事件的特定接口XXXListener; 事件源中产生事件后,必须注册相应于该类事件的监听器,使用addXXXListener(XXXListener )方法来注册监听器。
8
java.util.EventObject类
public class EventObject implements java.io.Serializable { protected transient Object source; public EventObject(Object source); public Object getSource(); public String toString(); }
9
java.awt.AWTEvent 和AWT有关的所有事件类都由java.awt.AWTEvent类派生 ,AWT事件共有10类,可以归为两大类:低级事件和高级事件。
10
低级事件 ComponentEvent(组件事件:组件尺寸的变化,移动) ContainerEvent(容器事件:组件增加,移动)
WindowEvent(窗口事件:关闭窗口,窗口闭合,图标化) FocusEvent(焦点事件:焦点的获得和丢失) KeyEvent(键盘事件:键按下、释放) MouseEvent(鼠标事件:鼠标单击,移动)
11
高级事件(语义事件) ActionEvent(动作事件:按钮按下,TextField中按Enter键)
AdjustmentEvent(调节事件:在滚动条上移动滑块以调节数值) ItemEvent(项目事件:选择项目,不选择项目) TextEvent(文本事件,文本对象改变)
12
AWT事件及其相应的监听器接口 ActionEvent 激活组件 ActionListener
actionPerformed(ActionEvent)
13
ItemEvent 选择了某些项目 ItemListener itemStateChanged(ItemEvent)
14
MouseEvent 鼠标移动 MouseMotionListener mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
15
MouseEvent 鼠标点击等 MouseListener mousePressed(MouseEvent)
mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent)
16
KeyEvent 键盘输入 KeyListener keyPressed(KeyEvent) keyReleased(KeyEvent)
keyTyped(KeyEvent)
17
FocusEvent 组件收到或失去焦点 FocusListener focusGained(FocusEvent)
focusLost(focusEvent)
18
AdjustementEvent 移动了滚动条等组件 AdjustmentListener
adjustmentValueChanged(AdjustmentEvent)
19
ComponentEvent 对象移动缩放显示隐藏等 ComponentListener
componentMoved(ComponentEvent) componentHidden(ComponentEvent) componentResized(ComponentEvent) componentShown(ComponentEvent)
20
WindowEvent 窗口收到窗口级事件 WindowListener windowClosing(WindowEvent)
windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent)
21
ContainerEvent 容器中增加删除了组件 ContainerListener
componentAdded(containerEvent) componentRemoved(containerEvent)
22
TextEvent 文本字段或文本区发生改变 TextListener textValueChanged(TextEvent)
23
事件处理举例
24
import java.awt.*; import java.awt.event.*; public class TwoListen implements MouseMotionListener,MouseListener,WindowListener { private Frame f; private TextField tf; public static void main(String args[]) { TwoListen two = new TwoListen(); two.go(); }
25
public void go() { f = new Frame("Two listeners example"); f.add(new Label("Click and drag the mouse"),"North"); tf = new TextField(30); f.add(tf,"South"); f.addMouseMotionListener(this); f.addMouseListener(this); f.addWindowListener(this); f.setSize(300,200); f.setVisible(true); }
26
public void mouseDragged (MouseEvent e) {
String s = "Mouse dragging : X="+e.getX()+"Y = "+e.getY(); tf.setText(s); } public void mouseMoved(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){ String s = "The mouse entered";
27
public void mouseExited(MouseEvent e){
String s = "The mouse has left the building"; tf.setText(s); } public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void windowClosing(WindowEvent e) { System.exit(1);
28
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} }
29
事件适配器(Event Adapters)
ComponentAdapter(组件适配器) ContainerAdapter(容器适配器) FocusAdapter(焦点适配器) KeyAdapter(键盘适配器) MouseAdapter(鼠标适配器) MouseMotionAdapter(鼠标运动适配器) WindowAdapter(窗口适配器)
Similar presentations