Presentation is loading. Please wait.

Presentation is loading. Please wait.

第五章 事件处理机制 Java 事件处理机制 窗口事件 鼠标事件 键盘事件 . . . 例:ButtonTest.java.

Similar presentations


Presentation on theme: "第五章 事件处理机制 Java 事件处理机制 窗口事件 鼠标事件 键盘事件 . . . 例:ButtonTest.java."— Presentation transcript:

1 第五章 事件处理机制 Java 事件处理机制 窗口事件 鼠标事件 键盘事件 例:ButtonTest.java

2 Java AWT事件模型工作机制 消息 监听器 事件 驱动程序 事件源

3 5.1事件处理基础 一.事件源:能够产生事件的对象都可以成为事件源。如文本框、按钮、鼠标、键盘等
一个事件源是一个能够注册监听器并且为监听器发送消息的一些对象 二.监听器:监听器对象会接收事件源对象发送的消息,并对发生的事件作出反应。 一个监听器对象就是一个实现了专门的监听器接口的类的对象,该类必须实现接口中的方法。 这些方法当事件发生时,被自动指行。

4 注册监听器方法 一、监听器类的定义 监听器类必须使用与事件源相对应的接口(例:ActionListener),从而必须 提供接口中的方法: actionPerformed(ActionEvent event) 的实现,此方法即是与事件相关联的事件驱动方法

5 Mylistener类的对象就可以用来作监听器
class Mylistener implements ActionListener {….    //使用了与事件相对应的接口 public void actionPerformed( ActionEvent event) { …..   //实现了接口里的方法 } Mylistener类的对象就可以用来作监听器

6 二.注册监听器 eventSourceObject.addEventListener( eventListenerObject) 例:
MyListener listener=new MyListener(); JButton button=new JButton(“ok”); button.addActionListener(Listener);

7 //监听器类 private class ColorAction implements ActionListener { ……. //事件驱动方法 public void actionPerformed(ActionEvent event) { setBackground(backgroundColor); repaint(); } private Color backgroundColor; } }

8 //创建监听器对象 ColorAction yellowAction = new ColorAction(Color.yellow); ColorAction blueAction = new ColorAction(Color.blue); ColorAction redAction = new ColorAction(Color.red); //注册监听器 yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); }

9 四.用匿名类简化程序 在例ButtonTest.java中,每个按钮需要同样的处理: 1.使用字符串构造按钮 2.把按钮添加到面板 3.用适当的颜色构造一个监听器 4.注册动作监听器

10 设计一个方法用来构造按钮: void makeButton(String name , color bcolor)
{JButton button=new JButton(name); add(button); Coloraction act=new //监听器对象 Coloraction(bcolor); button.addActionListener(act);   }

11 ButtonPanel构造器变为: makeButton(“yellow”,Color.yellow); makeButton(“blue”,Color.blue); makeButton(“red”,Color.red); 进一步简化,把ColorAction改写成匿名类 void makeButton(String name ,color bcolor) {JButton button=new JButton(name); add(button);

12 button.addActionListener(
    new ActionListener() {public void actionPerformed( actionEvent event) {setBackground(bcolor); repaint() } }); 例:NonameTest.java

13 5.3 窗口事件 与窗口相关的事件源是WindowEvent 窗口事件的监听器: WindowListener listener=…..
5.3 窗口事件 与窗口相关的事件源是WindowEvent 窗口事件的监听器:  WindowListener listener=…..     frame.addWindowListener(listener); 窗口监听器必须实现WindowListener接口 WindowListener接口中有七个方法,它们的名字是自解释的

14 一、WindowListener接口 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){};}

15 二、适配器类 每个具有不止一个方法的AWT监听器接口都有一个实现了它的所有方法,但却不做任何工作的适配器类。
例:WindowAdapter类 通过扩展适配器类来实现需要的动作 适配器类动态地满足了Java 中实现监视器类的技术要求

16 WindowAdapter类 扩展WindowAdapter类,继承六个空方法,重写WindowClosing():
class Terminator extends           WindowAdapter { public void windowClosing           (WindowEvent e)    System.exit(0); }    }

17 三、注册事件监听器 只要框架产生一个窗口事件,该事件就会传递给监听器对象 上面语句也可简化为:
WindowListener listener=new Terminator(); frame.addWindowListener(listener); 只要框架产生一个窗口事件,该事件就会传递给监听器对象 上面语句也可简化为: frame.addWindowListener(new Terminator());

18 用匿名类简化 frame.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);} } }) ;

19 四、AWT事件层次图     AWT事件类的继承关系

20 AWT事件类型 ActionEvent Item AdjustmentEvent KeyEvent
ComponentEvent MouseEvent ContainerEvent TextEvent FocusEvent WindowEvent

21 java.awt.event包中监听器接口 ActionListener KeyListener
AdjustmentListener ContainerListener ComponentListener TextListener FocusListener WindowListener MouseListener ItemListener MouseMotionListener

22 适配器 ComponentAdapter ContainerAdapter FocusAdapter KeyAdapter
MouseAdapter MouseMotionAdapter WindowAdapter

23 AWT中的事件分类 AWT明确区分两种事件: 语义事件和低级事件 语义事件:用于表达用户动作的事件。 如:点击按钮
   语义事件和低级事件 语义事件:用于表达用户动作的事件。 如:点击按钮 低级事件:把语义事件变为可能。如:对于一个按钮点击来说,它包括鼠标按下、鼠标移动和鼠标弹起低级事件 

24 语义事件 ActionEvent ItemEvent AdjustmentEvent TextEvent

25 低级事件 ComponentEvent MouseEvent ContainerEvent FocusEvent WindowEvent
KeyEvent

26 5.4 鼠标事件 1、鼠标事件有5种: 按下鼠标 释放鼠标 击鼠标键 鼠标进入 鼠标退出 一、鼠标事件(MouseListner接口)
5.4 鼠标事件 一、鼠标事件(MouseListner接口) 1、鼠标事件有5种:   按下鼠标   释放鼠标   击鼠标键   鼠标进入   鼠标退出  Painter.java

27 2. 鼠标事件返回值 鼠标事件的类型是MouseEvent 当发生鼠标事件时:
 MouseEvent类自动创建一个事件对象,以及事件发生位置的x和y坐标,作为事件返回值

28 3. MouseEvent类中的重要方法 处理事件源发生的事件接口是: MouseListener 事件源获得监视器的方法:
getX(),getY(), getPoint()  坐标位置  getModifiers(),isMetaDown() 左或右键 getClickCount() 被点击次数 处理事件源发生的事件接口是:   MouseListener 事件源获得监视器的方法:   addMouseListener

29 3. MouseListener接口中方法 MousePressed(MouseEvent)
MouseReleased(MouseEvent) MouseEntered(MouseEvent) MouseExited(MouseEvent) Mouseclicked(MouseEvent)

30 addMouseListener( new MouseListener() // anonymous inner class { public void mouseClicked( MouseEvent event ) } ……….. // event handler called when a mouse button is pressed public void mousePressed( MouseEvent event ) drawJPanelMousePressed( event );

31 private void drawJPanelMousePressed(
MouseEvent event ) { // store the location of the mouse currentPoint = event.getPoint(); if ( event.isMetaDown() ) // right mouse button is pressed drawColor = ERASE_COLOR; drawDiameter = ERASE_DIAMETER; } else // left mouse button is pressed drawColor = DRAW_COLOR; drawDiameter = DRAW_DIAMETER; repaint(); // repaint this DrawJPanel

32 二、鼠标移动事件(MouseMotionListener)
发生的事件:拖动鼠标、鼠标移动 鼠标事件的类型是MoseEvent 事件的接口是:   MouseMotionListener 获得监视器的方法:   addMouseMotionListener

33 MouseMotionListener接口中方法
mouseDragged(MouseEvent) mouseMoved(MouseEvent) addMouseMotionListener( new MouseMotionListener() // inner class { // event handler called when the mouse is dragged public void mouseDragged( MouseEvent event ) drawJPanelMouseDragged( event ); } ……….

34 例2:容器上的鼠标事件例 ContainerMouse 例3:组件上的鼠标事件例CompomentMouse
private void drawJPanelMouseDragged( MouseEvent event ) { // store the location of the mouse currentPoint = event.getPoint(); repaint(); // repaint this DrawJPanel } 例1:鼠标事件例MouseTest 例2:容器上的鼠标事件例 ContainerMouse 例3:组件上的鼠标事件例CompomentMouse

35 5.5 键盘事件 1、键盘事件: 按下KEY_PRESSED 释放KEY---RELEASE 敲击KEY---keyType
2、虚拟键代码:VK_键名    例如:VK_A代表A键       VK_0代表0键 打字训练器TypingApplication.java

36 3、注册键盘事件 事件的接口是:   KeyListener 获得监视器的方法:   addKeyListener

37 outputJTextArea.addKeyListener(
new KeyListener() // anonymous inner class { // event handler called when any key is pressed public void keyPressed( KeyEvent event ) outputJTextAreaKeyPressed( event ); } // event handler called when any key is released public void keyReleased( KeyEvent event ) outputJTextAreaKeyReleased( event ); } } // end anonymous inner class       ); // end ca

38 // get the key code for this event
private void outputJTextAreaKeyPressed(              KeyEvent event ) { // get the key code for this event int buttonIndex = event.getKeyCode(); // change the color of the associated JButton changeColor( keyJButtons[ buttonIndex ] ); }  // end method outputJTextAreaKeyPressed

39 // reset the color of the pressed key's JButton
private void outputJTextAreaKeyReleased(                KeyEvent event ) { resetColor(); }

40 KeyEvent常用方法 char getKeyEvent()返回键入字符 int getKeycode()返回虚拟代码
boolean isActionKey()是“动作”键,返回true.动作键如下:HOME、END、UP、F1--F12、PAUSE、ENTER……. static String getKeyModifiersText(int modifiers)返回描述修饰键的字符串

41 InputEvent常用方法 int getModifiers()返回一个整数,它的个位说明了SHIFT、CONTROL、ALT状态
boolean isAltdown() boolean isControlDown() boolean isMetaDown() boolean isShiftDown()

42 常用举例 例:下面代码用来测试SHIFT+右键是否按下 public void keyPressed(KeyEvent event)
{int ketcode=event.getKeyCode(); if(keycode==keyEvent.VK_RIGHT&& event.isShiftDown()) {…… } } 例:Sketch.java


Download ppt "第五章 事件处理机制 Java 事件处理机制 窗口事件 鼠标事件 键盘事件 . . . 例:ButtonTest.java."

Similar presentations


Ads by Google