隨筆 - 63  文章 - 0  trackbacks - 0
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類(lèi)

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          一個(gè)事件監(jiān)聽(tīng)機(jī)制(Event Listener)的例程

          事件監(jiān)聽(tīng)機(jī)制在java編程中有很重要的應(yīng)用,一般我們?cè)谔幚鞧UI編程時(shí),只是重寫(xiě)一下監(jiān)聽(tīng)接口的perform函數(shù)即可。但事件監(jiān)聽(tīng)在底層是如何運(yùn)行的?通過(guò)下面的例子我們可以有個(gè)清楚地了解。
          1.      首先寫(xiě)一個(gè)事件類(lèi)
          public class NewEvent {
               private int eventType=-1;
               public      NewEvent(int type){
                     eventType=type;
               }
               public int getEventType(){
                     return eventType;
               }

          }
          2.      寫(xiě)一個(gè)事件監(jiān)聽(tīng)器

          public interface INewEventListener {
                public void doProcessEvent(NewEvent event);

          }
          3.      寫(xiě)一個(gè)測(cè)試程序
          import java.util.ArrayList;
          import java.util.Iterator;
          import java.util.List;


          public class Source {
               private List listeners = new ArrayList();
               
               public void addNewEventListener(INewEventListener listener){//增加一個(gè)監(jiān)聽(tīng)器
                     if(!listeners.contains(listener)){
                           listeners.add(listener);
                     }
               }
               
               public void fireEvent(NewEvent event){//觸發(fā)事件
                     for (Iterator i=listeners.iterator(); i.hasNext();) {
                           INewEventListener listener = (INewEventListener)i.next();
                           listener.doProcessEvent(event);
                     }
               }
               
               public void GenerateEvent(int eventType){//生成一個(gè)指定類(lèi)型的事件
                     fireEvent(new NewEvent(eventType));
               }
               
               //for test
               public static void main (String [] args){
                     Source source = new Source ();
                     source.addNewEventListener(new INewEventListener(){//增加一個(gè)監(jiān)聽(tīng)器,并實(shí)現(xiàn)這個(gè)監(jiān)聽(tīng)器接口的方法
                            public void doProcessEvent(NewEvent event){
                                  int eventType = event.getEventType();
                                  System.out.println("EventType "+eventType+ " was triggered");
                            }
                     });
                     source.GenerateEvent(3);//生成一個(gè)eventType為3的事件
                     
               }
               
          }

          posted on 2009-04-05 08:39 lanxin1020 閱讀(171) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): j2se
          主站蜘蛛池模板: 平潭县| 仁布县| 手游| 普陀区| 琼海市| 得荣县| 灵川县| 茌平县| 社旗县| 平湖市| 宜春市| 连江县| 克什克腾旗| 浦城县| 图木舒克市| 琼中| 江津市| 太和县| 霞浦县| 萨嘎县| 东至县| 汝城县| 广西| 永吉县| 曲阜市| 墨脱县| 东丰县| 南乐县| 安陆市| 伊吾县| 天全县| 新营市| 岳普湖县| 广灵县| 滦南县| 连山| 佛山市| 禹州市| 临城县| 理塘县| 灌阳县|