sunfruit[請訪問http://www.fruitres.cn]

          --我相信JAVA能走得更遠 QQ:316228067

          [原創]JAVA用戶自定義事件監聽完整例子

              --sunfruit

              很多介紹用戶自定義事件都沒有例子,或是例子不全,下面寫了一個完整的例子,并寫入了注釋以便參考,完整的實例源代碼如下:

          package demo;

          import java.util.EventObject;

          /**
           * Title: 事件處理類,繼承了事件基類
           * Description: 
           * Copyright: Copyright (c) 2005
           * Company: cuijiang
           * @author not attributable
           * @version 1.0
           */
          public class DemoEvent extends EventObject
          {
            private Object obj;
            private String sName;
            public DemoEvent(Object source,String sName)  {
              super(source);
              obj = source;
              this.sName=sName;
            }
            public Object getSource()
            {
              return obj;
            }
            public void say()
            {
              System.out.println("這個是 say 方法...");
            }

            public String getName()
            {
              return sName;
            }
          }

          package demo;

          import java.util.EventListener;

          /**
           * Title: 監聽器接口
           * Description: 
           * Copyright: Copyright (c) 2005
           * Company: cuijiang
           * @author not attributable
           * @version 1.0
           */
          public interface DemoListener extends EventListener{
            public void demoEvent(DemoEvent dm);
          }

          package demo;

          import java.util.*;

          /**
           *Title: 使用事件的類
           * Description: 該類實現了監聽器的添加和監聽器方法的執行,并且實現了由于屬性的改變而執行事件
           * Description: 在添加、刪除、執行監聽器的時候都要注意同步問題
           * Copyright: Copyright (c) 2005
           * Company: cuijiang
           * @author not attributable
           * @version 1.0
           */
          public class DemoSource{
            private Vector repository = new Vector();
            private DemoListener dl;
            private String sName="";

            public DemoSource()
            {
            }

            //注冊監聽器,如果這里沒有使用Vector而是使用ArrayList那么要注意同步問題
            public void addDemoListener(DemoListener dl)
            {
              repository.addElement(dl);//這步要注意同步問題
            }

            //如果這里沒有使用Vector而是使用ArrayList那么要注意同步問題
            public void notifyDemoEvent(DemoEvent event) {
              Enumeration enum = repository.elements();//這步要注意同步問題
              while(enum.hasMoreElements())
              {
                dl = (DemoListener)enum.nextElement();
                dl.demoEvent(event);
              }
            }

            //刪除監聽器,如果這里沒有使用Vector而是使用ArrayList那么要注意同步問題
            public void removeDemoListener(DemoListener dl)
            {
              repository.remove(dl);//這步要注意同步問題
            }

            /**
             * 設置屬性
             * @param str1 String
             */
            public void setName(String str1)
            {
              boolean bool=false;
              if(str1==null && sName!=null) bool=true;
              else if(str1!=null && sName==null) bool=true;
              else if(!sName.equals(str1)) bool=true;

              this.sName=str1;

              //如果改變則執行事件
              if(bool) notifyDemoEvent(new DemoEvent(this,sName));
            }

            public String getName()
            {
              return sName;
            }
          }

          package demo;

          import java.lang.Thread;

          /**
           * Title: 測試類
           * Description: 測試了由于改變屬性而引起的事件發生
           * Copyright: Copyright (c) 2005
           * Company: cuijiang
           * @author not attributable
           * @version 1.0
           */
          public class TestDemo
              implements DemoListener {
            private DemoSource ds;

            public TestDemo()
            {
              ds=new DemoSource();
              ds.addDemoListener(this);
              System.out.println("添加監聽器完畢");
              try {
                Thread.sleep(3000);
                //改變屬性,觸發事件
                ds.setName("改變屬性,觸發事件");
              }
              catch (InterruptedException ex) {
                ex.printStackTrace();
              }

              ds.addDemoListener(this);
              System.out.println("添加監聽器完畢2");
              try {
                Thread.sleep(3000);
                //改變屬性,觸發事件
                ds.setName("改變屬性,觸發事件2");
              }
              catch (InterruptedException ex) {
                ex.printStackTrace();
              }

              ds.removeDemoListener(this);
              System.out.println("添加監聽器完畢3");
              try {
                Thread.sleep(3000);
                //改變屬性,觸發事件
                ds.setName("改變屬性,觸發事件3");
              }
              catch (InterruptedException ex) {
                ex.printStackTrace();
              }


            }

            public static void main(String args[])
            {

              new TestDemo();
            }

            /**
             * demoEvent
             * @param dm DemoEvent
             * @todo Implement this test.DemoListener method
             */
            public void demoEvent(DemoEvent dm) {
              System.out.println("事件處理方法");
              System.out.println(dm.getName());
              dm.say();
            }
          }

          posted on 2006-02-19 17:59 sunfruit 閱讀(858) 評論(0)  編輯  收藏 所屬分類: JAVA SE & EE

          主站蜘蛛池模板: 夏津县| 鸡东县| 辽宁省| 威远县| 宜兴市| 静乐县| 钟祥市| 屏边| 林州市| 天台县| 江口县| 文安县| 邵东县| 湘乡市| 施甸县| 云南省| 江口县| 青神县| 鄯善县| 德保县| 威信县| 竹北市| 西安市| 珠海市| 林口县| 无极县| 大竹县| 浙江省| 深州市| 城口县| 邢台市| 余干县| 镇宁| 淮安市| 嘉黎县| 昌江| 呼玛县| 永嘉县| 当阳市| 龙川县| 巴南区|