zoukankan      html  css  js  c++  java
  • EventListenerList举例

    1. package testCustomEvent;  
    2.   
    3. import javax.swing.event.EventListenerList;     
    4. import java.util.Date;     
    5. import java.awt.event.ActionEvent;  
    6. import java.awt.event.ActionListener;  
    7. import java.text.DateFormat;     
    8. import java.text.SimpleDateFormat;    
    9.     
    10. /**   
    11. * Java的事件机制/自定义事件.  
    12.   
    13. 运行结果:   
    14. do something interesting in source here.   
    15. listener detects [event]:wintys.event.MyEvent[source=wintys.event.MySource@18158   
    16. 59] [occur at]:2012-10-11 16:27:49   
    17. listener detects [event]:wintys.event.MyEvent[source=wintys.event.MySource@18158   
    18. 59] [occur at]:2012-10-11 16:27:49  
    19.   
    20. * @version 2012-10-11   
    21. * @author germmy(gogogo@gmail.com)   
    22. * @see http://www.blogjava.net/gogogo   
    23. */     
    24. public class MyEventTestNew implements MyListener{     
    25.     public static void main(String[] args){     
    26.         MySource source = new MySource();   
    27.           
    28.         ActionListener al=new ActionListener(){  
    29.   
    30.             @Override  
    31.             public void actionPerformed(ActionEvent e) {  
    32.                 // TODO Auto-generated method stub  
    33.                 System.out.println("allllllllllllllll");  
    34.             }  
    35.               
    36.         };  
    37.           
    38.         MyEventTestNew mtn=new MyEventTestNew();  
    39.         source.addMyListener(mtn);   
    40.         source.addMyListener(al);  
    41.         source.doSomething();     
    42.     }  
    43.   
    44.     @Override  
    45.     public void doMyAction(MyEvent e) {  
    46.          System.out.println("listener detects " + e);     
    47.           
    48.     }     
    49. }    
    50.     
    51. /**   
    52. * 自定义的事件.   
    53. * @version 2012-10-11   
    54. * @author germmy(gogogo@gmail.com)   
    55. * @see http://www.blogjava.net/gogogo   
    56. */     
    57. class MyEvent extends java.util.EventObject{     
    58.     private Date date;//记录事件发生的时间    
    59.     
    60.     public MyEvent(Object source , Date date){     
    61.         super(source);    
    62.     
    63.         this.date = date;     
    64.     }    
    65.     
    66.     public String toString(){     
    67.         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     
    68.         String dt = df.format(date);    
    69.     
    70.         return "[event]:" + super.toString() + " [occur at]:" + dt;     
    71.     }     
    72. }    
    73.     
    74. /**   
    75. * 自定义事件监听器接口.   
    76. * @version 2012-10-11   
    77. * @author germmy(gogogo@gmail.com)   
    78. * @see http://www.blogjava.net/gogogo   
    79. */     
    80. interface MyListener extends java.util.EventListener{     
    81.     void doMyAction(MyEvent e);     
    82. }    
    83.     
    84. /**   
    85. * 自定义事件源.   
    86. * @version 2012-10-11   
    87. * @author germmy(gogogo@gmail.com)   
    88. * @see http://www.blogjava.net/gogogo   
    89. */     
    90. class MySource{     
    91.     /**   
    92.      * 保存注册的监听器列表.   
    93.      * 子类可以使用它保存自己的事件监听器(非MyListener监听器)列表.   
    94.      */     
    95.     protected EventListenerList listenerList = new EventListenerList();     
    96.     private MyEvent myEvent = null;//fireDoMyAction()使用此变量    
    97.     
    98.     /**   
    99.      * 没有做任何事   
    100.      */     
    101.     public MySource(){     
    102.     }     
    103.     /**   
    104.      * 添加一个MyListener监听器   
    105.      */     
    106.     public void addMyListener(MyListener listener){     
    107.         listenerList.add(MyListener.class , listener);     
    108.     }    
    109.       
    110.     /**   
    111.      * 添加一个MyListener监听器   
    112.      */     
    113.     public void addMyListener(ActionListener al){     
    114.         listenerList.add(ActionListener.class , al);     
    115.     }    
    116.     
    117.     /**   
    118.      * 移除一个已注册的MyListener监听器.   
    119.      * 如果监听器列表中已有相同的监听器listener1、listener2,   
    120.      * 并且listener1==listener2,   
    121.      * 那么只移除最近注册的一个监听器。   
    122.      */     
    123.     public void removeMyListener(MyListener listener){     
    124.         listenerList.remove(MyListener.class , listener);     
    125.     }    
    126.     
    127.     /**   
    128.      * @return 在此对象上监听的所有MyListener类型的监听器   
    129.      */     
    130.     public MyListener[] getMyListeners(){     
    131.         return (MyListener[])listenerList.getListeners(MyListener.class);     
    132.     }    
    133.     
    134.       
    135.      */     
    136.     protected void fireDoMyAction() {     
    137.          // getListenerList() Guaranteed to return a non-null array     
    138.          Object[] listeners = listenerList.getListenerList();     
    139.          // Process the listeners last to first, notifying     
    140.          // those that are interested in this event     
    141.         for (int i = listeners.length-2; i>=0; i-=2) {     
    142.             if (listeners[i]==MyListener.class) {     
    143.                 // Lazily create the event:     
    144.                 if (myEvent == null)     
    145.                     myEvent = new MyEvent(this , new Date());     
    146.                 ((MyListener)listeners[i+1]).doMyAction(myEvent);     
    147.             }  else if(listeners[i]==ActionListener.class) {  
    148.                 System.out.println("hahahahahaha");  
    149.             }  
    150.         }     
    151.     }    
    152.     
    153.     /**   
    154.      * 做一些事件源应该做的有意义的事,然后通知监听器.   
    155.      * 这里只是一个示例方法.   
    156.      * 例如:MySource如果是一个按钮,则doSomething()就可以命名为click(),   
    157.      * 当用户点击按钮时调用click()方法.   
    158.      */     
    159.     public void doSomething() {     
    160.         System.out.println("do something interesting here.");    
    161.     
    162.         fireDoMyAction();//通知监听器     
    163.     }     
    164. }  
  • 相关阅读:
    mysql sql语句大全
    windows composer 安装,使用新手入门
    PHP 变量类型的强制转换 & 创建空对象
    window bat 运行 cmd 命令
    window apidoc的安装和使用
    linux apidoc的安装和使用
    RabbitMQ的安装与基本使用
    控制流之continue
    控制流之break
    控制流之while
  • 原文地址:https://www.cnblogs.com/ygzs007/p/9041768.html
Copyright © 2011-2022 走看看