java自定義監聽器的示例代碼

JAVA用戶自定義事件監聽完整例子

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

}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章