物件導向程式設計

 

 

第六章、設計Java Applet

 

 


授課教師:陳慶瀚

WWW : http://www.miat.ee.isu.edu.tw/java

E-mail : pierre@isu.edu.tw   

 


     

    6.8 事件驅動程式設計

 6.8.1 委任式事件模型(Delegation Event Model)

一個source可以產生和送出Event給一個或多個Listener,每一個Listener總是隨時等待接收event,一旦接收到一個event,Listener就會立即處理它並傳回結果。任何一個使用者介面元件都可以委任這個程序模型來處理事件。

event(事件):

event是一種用來描述狀態改變的物件。當使用者與GUI介面連續互動時,將會有一系列的event產生。例如按下一個button,從鍵盤輸入一個字元,從一個list box選擇一個item,或是click一下滑鼠。

event source(事件來源):

source是一個產生event的物件,它可以產生一種或多種不同的事件,一個Source必須先註冊(register)才能夠委任listener來處理它的事件,每一個event都有它自己的註冊方法(成員函式),標準型態為:

public void addTypeListener(TypeListener el)

例如註冊一個鍵盤事件listener的成員函式為:

    addKeyListener( )

而註冊一個滑鼠移動事件的listener的成員函式為:

   addMouseMotionListener( )

某些source可以允許註冊多種listener,其型態為:

    public void addTypeListener(TypeListener el)

如果想要解除註冊,可以使用

    public void removeTypeListener(TypeListener el)

event source(事件來源):

一個listener是一個等待被event通知的物件。使用listener有兩個條件,第一listener必須先被註冊才能使用。第二必須定義相關成員函式以便接收和處理事件。所有listener的成員函式都宣告在java.awt.event的package中。

6.8.2 event物件類別

所有Java語言的event物件類別都繼承自EventObject ,包括awt所有GUI元件的event物件類別AWTEvent

EventObject 有兩個成員函式: getSource( )toString( ).

 

java.awt.event 主要的event事件類別

 

 

Event Class

Description

 

ActionEvent

Generated when a button is pressed, a list item is double-clicked, or a menu item is selected.

AdjustmentEvent

Generated when a scroll bar is manipulated.

ComponentEvent

Generated when a component is hidden, moved, resized, or becomes visible.

ContainerEvent

Generated when a component is added to or removed from a container.

FocusEvent

Generated when a component gains or loses keyboard focus.

InputEvent

Abstract super class for all component input event classes.

ItemEvent

Generated when a check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselected.

KeyEvent

Generated when input is received from the keyboard.

MouseEvent

Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component.

TextEvent

Generated when the value of a text area or text field is changed.

WindowEvent

Generated when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.

6.8.3 Sources of Events物件類別

 

 

Event Source

Description

 

Button

Generates action events when the button is pressed.

Checkbox

Generates item events when the check box is selected or deselected.

Choice

Generates item events when the choice is changed.

List

Generates action events when an item is double-clicked; generates item events when an item is selected or deselected.

Menu Item

Generates action events when a menu item is selected; generates item events when a checkable menu item is selected or deselected.

Scrollbar

Generates adjustment events when the scroll bar is manipulated.

Text components

Generates text events when the user enters a character.

Window

Generates window events when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.

6.8.4 Event Listener物件類別: Interfaces

 

 

Event Listener Interfaces

 

Interface

Description

 

ActionListener

Defines one method to receive action events.

AdjustmentListener

Defines one method to receive adjustment events.

ComponentListener

Defines four methods to recognize when a component is hidden, moved, resized, or shown.

ContainerListener

Defines two methods to recognize when a component is added to or removed from a container.

FocusListener

Defines two methods to recognize when a component gains or loses keyboard focus.

ItemListener

Defines one method to recognize when the state of an item changes.

KeyListener

Defines three methods to recognize when a key is pressed, released, or typed.

MouseListener

Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released.

MouseMotionListener

Defines two methods to recognize when the mouse is dragged or moved.

TextListener

Defines one method to recognize when a text value changes.

WindowListener

Defines seven methods to recognize when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.

6.8.5 滑鼠事件驅動程式設計

為了掌握滑鼠得事件,我們需定義兩個物件類別:

─ MouseListener interfaces

─ MouseMotionListener interfaces

下列範例程式用來展示滑鼠事件的處理:

// Demonstrate the mouse event handlers.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
public class ex9 extends Applet
  implements MouseListener, MouseMotionListener
{
 
  String msg = "";
  int mouseX = 0, mouseY = 0; // coordinates of mouse
 
  public void init()
  {
     addMouseListener(this);
     addMouseMotionListener(this);
  }
 
  // Handle mouse clicked.
  public void mouseClicked(MouseEvent me)
  {
    // save coordinates
    mouseX = 0;
    mouseY = 10;
    msg = "滑鼠咬我!";
    repaint();
  }
 
  // Handle mouse entered.
  public void mouseEntered(MouseEvent me)
  {
    // save coordinates
    mouseX = 0;
    mouseY = 10;
    msg = "滑鼠進來了!";
    repaint();
  }
 
  // Handle mouse exited.
  public void mouseExited(MouseEvent me)
  {
    // save coordinates
    mouseX = 0;
    mouseY = 10;
    msg = "滑鼠出去了!";
    repaint();
  }
 
  // Handle button pressed.
  public void mousePressed(MouseEvent me)
  {
    // save coordinates
    mouseX = me.getX();
    mouseY = me.getY();
    msg = "按下";
    repaint();
  }

  // Handle button released.
  public void mouseReleased(MouseEvent me)
  {
    // save coordinates
    mouseX = me.getX();
    mouseY = me.getY();
    msg = "放開";
    repaint();
  }
 
  // Handle mouse dragged.
  public void mouseDragged(MouseEvent me)
  {
    // save coordinates
    mouseX = me.getX();
    mouseY = me.getY();
    msg = "*";
    showStatus("滑鼠現在位置:" + mouseX + ", " + mouseY);
    repaint();
  }
 
  // Handle mouse moved.
  public void mouseMoved(MouseEvent me)
  {
    // show status
    showStatus("滑鼠現在位置:" + me.getX() + ", " + me.getY());
  }
 
  // Display msg in applet window at current X,Y location.
  public void paint(Graphics g)
  {
    g.drawString(msg, mouseX, mouseY);
  }    
}

6.8.6 Keyboard事件處理

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
public class SimpleKey extends Applet implements KeyListener
{
 
  String msg = "";
  int X = 10, Y = 20; // output coordinates
 
  public void init()
  {
    addKeyListener(this);
    requestFocus(); // request input focus
  }
 
  public void keyPressed(KeyEvent ke)
  {
    showStatus("Key按下");
  }
 
  public void keyReleased(KeyEvent ke)
  {
    showStatus("Key放開");
  }
 
  public void keyTyped(KeyEvent ke)
  {
    msg += ke.getKeyChar();
    repaint();
  }
 
  // Display keystrokes.
  public void paint(Graphics g)
  {
    g.drawString(msg, X, Y);
  }    
}

如果想使用一些特殊的功能鍵,如<F1>, <F2>,...,<PgDn>, <PgUp>...,範例程式如下:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class KeyEvents extends Applet implements KeyListener
{
 
  String msg = "";
  int X = 10, Y = 20; // output coordinates
 
  public void init()
  {
    addKeyListener(this);
    requestFocus(); // request input focus
  }
 
  public void keyPressed(KeyEvent ke)
  {
    showStatus("Key按下");
 
    int key = ke.getKeyCode();
    switch(key)
    {
      case KeyEvent.VK_F1:
        msg += "<F1>";
        break;
      case KeyEvent.VK_F2:
        msg += "<F2>";
        break;
      case KeyEvent.VK_F3:
        msg += "<F3>";
        break;
      case KeyEvent.VK_PAGE_DOWN:
        msg += "<PgDn>";
        break;
      case KeyEvent.VK_PAGE_UP:
        msg += "<PgUp>";
        break;
      case KeyEvent.VK_LEFT:
        msg += "<Left Arrow>";
        break;
      case KeyEvent.VK_RIGHT:
        msg += "<Right Arrow>";
        break;
    }
    repaint();
  }
 
  public void keyReleased(KeyEvent ke)  
  {
    showStatus("Key放開");
  }
 
  public void keyTyped(KeyEvent ke)
  {
    msg += ke.getKeyChar();
    repaint();
  }
 
  // Display keystrokes.
  public void paint(Graphics g)
  {
    g.drawString(msg, X, Y);
  }    
}

 


 

物件導向程式設計

義守大學電機系 陳慶瀚

2001.12.18