您的位置:首页 > 编程语言 > Java开发

JAVA事件及监听器

2011-06-23 10:12 330 查看
 



【文章转载自http://blog.sina.com.cn/weiliang516
,转载请注明出处】

与AWT有关的所有事件类都由java.awt.AWTEvent类派生,它也是EventObject类的子类。AWT事件共有10类,可以归为两大类:低级事件和高级事件。

  
java.util.EventObject类是所有事件对象的基础父类,所有事件都是由它派生出来的。AWT的相关事件继承于
java.awt.AWTEvent类,这些AWT事件分为两大类:低级事件和高级事件,低级事件是指基于组件和容器的事件,当一个组件上发生事件,如:
鼠标的进入,点击,拖放等,或组件的窗口开关等,触发了组件事件。高级事件是基于语义的事件,它可以不和特定的动作相关联,而依赖于触发此事件的类,如在
TextField中按Enter键会触发ActionEvent事件,滑动滚动条会触发AdjustmentEvent事件,或是选中项目列表的某一条
就会触发ItemEvent事件。

  
◇ 低级事件

  ComponentEvent( 组件事件:组件尺寸的变化,移动)

  ContainerEvent( 容器事件:组件增加,移动)

  WindowEvent( 窗口事件:关闭窗口,窗口闭合,图标化)

  FocusEvent( 焦点事件:焦点的获得和丢失)

  KeyEvent( 键盘事件:键按下、释放)

  MouseEvent( 鼠标事件:鼠标单击,移动)

  

高级事件(语义事件)


  ActionEvent(动作事件:按钮按下,TextField中按Enter键)

  AdjustmentEvent(调节事件:在滚动条上移动滑块以调节数值)

  ItemEvent(项目事件:选择项目,不选择"项目改变")

  TextEvent(文本事件,文本对象改变)

 事件监听器



  每类事件都有对应的事件监听器,监听器是接口,根据动作来定义方法。

  例如,与键盘事件KeyEvent相对应的接口是:

  public interface KeyListener extends EventListener {

     public void keyPressed(KeyEvent ev);

     public void keyReleased(KeyEvent ev);

     public void keyTyped(KeyEvent ev);

  }

  注意到在本接口中有三个方法,那么java运行时系统何时调用哪个方法?其实根据这三个方法的方法名就能够知道应该是什么时候调用哪个方法执行了。当键盘刚按下去时,将调用keyPressed(
)方法执行,当键盘抬起来时,将调用keyReleased(
)方法执行,当键盘敲击一次时,将调用keyTyped(
)方法执行。

  又例如窗口事件接口:

  public interface WindowListener extends EventListener{

     public void windowClosing(WindowEvent e);

     //把退出窗口的语句写在本方法中

     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);

     //窗口非激活时调用

  }

  AWT的组件类中提供注册和注销监听器的方法:

  ◇ 注册监听器:

  public void add<ListenerType>
(<ListenerType>listener);

  ◇ 注销监听器:

  public void remove<ListenerType>
(<ListenerType>listener);
  例如Button类:(查API)

  public class Button extends Component {

     ……

     public synchronized void addActionListener(ActionListener
l);

     public synchronized void
removeActionListener(ActionListener l);

     ……}

  表5.1列出了所有AWT事件及其相应的监听器接口,一共10类事件,11个接口。下面这张表应能牢牢记住。

表5.1

?/P>
事件类别

描述信息

接口名

方法

 ActionEvent激活组件  ActionListener actionPerformed(ActionEvent)
 ItemEvent选择了某些项目  ItemListener itemStateChanged(ItemEvent)
 MouseEvent鼠标移动  MouseMotionListener mouseDragged(MouseEvent)

 mouseMoved(MouseEvent)
鼠标点击等  MouseListener mousePressed(MouseEvent)

 mouseReleased(MouseEvent)

 mouseEntered(MouseEvent)

 mouseExited(MouseEvent)

 mouseClicked(MouseEvent)
 KeyEvent键盘输入  KeyListener keyPressed(KeyEvent)

 keyReleased(KeyEvent)

 keyTyped(KeyEvent)
 FocusEvent组件收到或失去焦点  FocusListener focusGained(FocusEvent)

 focusLost(FocusEvent)
 AdjustmentEvent移动了滚动条等组件  AdjustmentListener adjustmentValueChanged(AdjustmentEvent)
 ComponentEvent对象移动缩放显示隐藏等  ComponentListener componentMoved(ComponentEvent)

 componentHidden(ComponentEvent)

 componentResized(ComponentEvent)

 componentShown(ComponentEvent)
 WindowEvent窗口收到窗口级事件  WindowListener windowClosing(WindowEvent)

 windowOpened(WindowEvent)

 windowIconified(WindowEvent)

 windowDeiconified(WindowEvent)

 windowClosed(WindowEvent)

 windowActivated(WindowEvent)

 windowDeactivated(WindowEvent)
 ContainerEvent容器中增加删除了组件  ContainerListener componentAdded(ContainerEvent)

 componentRemoved(ContainerEvent)
 TextEvent文本字段或文本区发生改变  TextListener textValueChanged(TextEvent)
5.2.3
AWT事件及其相应的监听器接口(2)


  例5.10说明事件处理模型的应用。

例5.10

  import java.awt.*;

  import java.awt.event.*;

    public class ThreeListener implements
MouseMotionListener,MouseListener,WindowListener {

    //实现了三个接口

    private Frame f;

    private TextField tf;

    public static void main(String args[])

    {

     ThreeListener two = new ThreeListener();

     two.go(); }

    public void go() {

    f = new Frame("Three listeners example");

    f.add(new Label("Click and drag the mouse"),"North");

    tf = new TextField(30);

    f.add(tf,"South"); //使用缺省的布局管理器

    f.addMouseMotionListener(this); //注册监听器MouseMotionListener

    f.addMouseListener(this); //注册监听器MouseListener

    f.addWindowListener(this); //注册监听器WindowListener

    f.setSize(300,200);

    f.setVisible(true);

      }
    public void mouseDragged (MouseEvent
e) {

    //实现mouseDragged方法

    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";

      tf.setText(s);

        }
    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) {

    //为了使窗口能正常关闭,程序正常退出,需要实现windowClosing方法

      System.exit(1);

        }
    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) {}
    }
  上例中有如下几个特点:

  1.可以声明多个接口,接口之间用逗号隔开。

    ……implements MouseMotionListener, MouseListener,
WindowListener;

  

  2.可以由同一个对象监听一个事件源上发生的多种事件:

  f.addMouseMotionListener(this);

  f.addMouseListener(this);

  f.addWindowListener(this);

  则对象f 上发生的多个事件都将被同一个监听器接收和处理。

  3.事件处理者和事件源处在同一个类中。本例中事件源是Frame
f,事件处理者是类ThreeListener,其中事件源Frame
f是类ThreeListener的成员变量。

  4.可以通过事件对象获得详细资料,比如本例中就通过事件对象获得了鼠标发生时的坐标值。

  public void mouseDragged(MouseEvent e) {

   String s="Mouse dragging :X="+e.getX()+"Y="+e.getY();

   tf.setText(s);

  }

  Java语言类的层次非常分明,因而只支持单继承,为了实现多重继承的能力,Java用接口来实现,一
个类可以实现多个接口,这种机制比多重继承具有更简单、灵活、更强的功能。在AWT中就经常用到声明和实现多个接口。记住无论实现了几个接口,接口中已定
义的方法必须一一实现,如果对某事件不感兴趣,可以不具体实现其方法,而用空的方法体来代替。但却必须所有方法都要写上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息