zoukankan      html  css  js  c++  java
  • 关于事件监听机制的总结(Listener和Adapter)

    记得以前看过事件监听机制背后也是有一种设计模式的.(设计模式的名字记不清了,只记得背后实现的数据结构是数组.)

    附上事件监听机制的分析图:

    一个事件源可以承载多个事件(只要这个事件源支持这个事件就可以,男人就不支持生孩子的事件)  事件和监听器有对应关系的.

    下面用awt中的标准图形化界面分析:

    接口WindowListener 中有一个实现类WindowAdapter类.适配器类.

    EventListener.java 下面是所有超级接口EventListener.java的源代码   其实就是一个空接口.

    1 package java.util;
    2 
    3 /**
    4  * A tagging interface that all event listener interfaces must extend.
    5  * @since JDK1.1
    6  */
    7 public interface EventListener {
    8 }

    WindowListener.java 这个接口的源代码  定义了一些抽象方法

     1 public interface WindowListener extends EventListener {
     2     /**
     3      * Invoked the first time a window is made visible.
     4      */
     5     public void windowOpened(WindowEvent e);
     6 
     7     public void windowClosing(WindowEvent e);
     8 
     9     public void windowClosed(WindowEvent e);
    10 
    11     public void windowIconified(WindowEvent e);
    12 
    13     public void windowDeiconified(WindowEvent e);
    14 
    15     public void windowActivated(WindowEvent e);
    16 
    17     public void windowDeactivated(WindowEvent e);
    18 }

    WindowAdapter.java这个实现类的源代码: WindowAdapter是一个抽象类.但是这个抽象类里面却没有抽象方法!!!!!!!

    所有的方法都是空实现!!!空方法!!!

     1 public abstract class WindowAdapter
     2     implements WindowListener, WindowStateListener, WindowFocusListener
     3 {
     4     /**
     5      * Invoked when a window has been opened.
     6      */
     7     public void windowOpened(WindowEvent e) {}
     8 
     9     public void windowClosing(WindowEvent e) {}
    10 
    11     public void windowClosed(WindowEvent e) {}
    12 
    13     public void windowIconified(WindowEvent e) {}
    14 
    15     public void windowDeiconified(WindowEvent e) {}
    16 
    17     public void windowActivated(WindowEvent e) {}
    18 
    19     public void windowDeactivated(WindowEvent e) {}
    20 
    21     public void windowStateChanged(WindowEvent e) {}
    22 
    23     public void windowGainedFocus(WindowEvent e) {}
    24 
    25     /**
    26      * Invoked when the Window is no longer the focused Window, which means
    27      * that keyboard events will no longer be delivered to the Window or any of
    28      * its subcomponents.
    29      *
    30      * @since 1.4
    31      */
    32     public void windowLostFocus(WindowEvent e) {}
    33 }

    窗口事件的抽象适配器类(WindowAdapter).此类中的方法为空.此类存在的目的是方便创建监听器对象.

    如果实现WindowListener 就要覆盖其中的所有抽象方法,但是继承WindowAdapter就不需要了,JDK为你定义好了这样的一个适配器类,

    实现了所有WindowListener接口中的抽象方法.所以只需继承WindowAdapter类就可以了.需要什么方法就覆盖其中的方法.

    EventListener是接口,WindowListener 也是接口,WindowAdapter是一个抽象类.

    EventListener接口是一个空接口,WindowListener接口定义了一些公用的抽象方法,WindowAdapter是一个抽象类,但是里面没有任何的抽象方法.

    要深入理解抽象类和接口的区别.

    抽象类可以不含有抽象方法的!!!

    因为这样有两个好处.

    1.不让该类创建对象.

    2.方便创建该接口的对象.

    继承我这个抽象类,选择其中的一个方法去覆盖就可以了

    在其中直接用匿名内部类就可以了.用哪个覆盖哪个.

    给一个按钮增加一个活动监听.ActionListener

    在Button类中有一个addActionListener(Listener l)  添加指定的动作侦听器,以接收发自此按钮的动作事件。

     

    其中要传入一个ActionListener对象.看一下这个ActionListener对象

    这个ActionListener对象有N多实现类,但是却不像WindowsLisntener一样有一个对应的适配器类.因为这个ActionListener接口中只有一个方法.

    这就是一种原则,规则性的问题.

    只要监听器中的方法超过两个一般都有一个对应的适配器,但是这个ActionListener接口中只有一个方法.

    监听器中有两个或者两个以内方法的话没有对应适配器类的,创建本身就很方便.

    addActionListener(ActionListener l)方法参数传递的是接口型引用,ActionListener本身方法又不多(就只有一个方法,只要覆写一个或者两个就可以了),

    方法参数中用匿名内部类就可以了,这样表示看着更简洁明了.

    窗体监听(WindowListener)和活动监听(ActionListener)

     下面附上代码Demo 

     1 import java.awt.Button;
     2 import java.awt.FlowLayout;
     3 import java.awt.Frame;
     4 import java.awt.event.ActionEvent;
     5 import java.awt.event.ActionListener;
     6 import java.awt.event.WindowAdapter;
     7 import java.awt.event.WindowEvent;
     8 
     9 public class FrameDemo {
    10     public static void main(String[] args) {
    11 
    12         Frame f = new Frame("my frame");
    13         f.setBounds(400, 200, 500, 400);        
    14         f.setLayout(new FlowLayout());//设置流式布局
    15         
    16         Button but  = new Button("一个按钮");
    17         f.add(but);//将按钮添加到窗体中。 
    18         
    19         f.addWindowListener(new WindowAdapter() {
    20             @Override
    21             public void windowClosing(WindowEvent e) {
    22                 //System.out.println("closing......."+e);
    23                 System.exit(0);
    24             }
    25         });
    26         //在按钮上加上一个监听。  点击按钮"一个按钮"就关闭
    27         but.addActionListener(new ActionListener() {
    28             @Override
    29             public void actionPerformed(ActionEvent e) {
    30                 //System.out.println("button run .....");
    31                 System.exit(0);
    32             }
    33         });
    34         f.setVisible(true);
    35         System.out.println("over");
    36     }
    37 }

    点击"右上角的×"和"一个按钮"都是关闭的效果.

  • 相关阅读:
    BEM(Block–Element-Modifier)
    http://element.eleme.io/#/zh-CN/component/quickstart
    Commit message 的写法规范。本文介绍Angular 规范(
    好的commit应该长啥样 https://github.com/torvalds/linux/pull/17#issuecomment-5654674
    代码管理
    if you have content fetched asynchronously on pages where SEO is important, SSR might be necessary
    Martin Fowler’s Active Record design pattern.
    The Zen of Python
    Introspection in Python How to spy on your Python objects Guide to Python introspection
    Object-Oriented Metrics: LCOM 内聚性的度量
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4161632.html
Copyright © 2011-2022 走看看