JAVA用戶自定義事件監(jiān)聽(tīng)實(shí)例代碼
更新時(shí)間:2017年04月18日 16:37:13 投稿:lqh
這篇文章主要介紹了JAVA用戶自定義事件監(jiān)聽(tīng)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
JAVA用戶自定義事件監(jiān)聽(tīng)實(shí)例代碼
很多介紹用戶自定義事件都沒(méi)有例子,或是例子不全,下面寫了一個(gè)完整的例子,并寫入了注釋以便參考,完整的實(shí)例源代碼如下:
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("這個(gè)是 say 方法..."); } public String getName() { return sName; } }
package demo; import java.util.EventListener; /** * Title: 監(jiān)聽(tīng)器接口 * 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: 該類實(shí)現(xiàn)了監(jiān)聽(tīng)器的添加和監(jiān)聽(tīng)器方法的執(zhí)行,并且實(shí)現(xiàn)了由于屬性的改變而執(zhí)行事件 * Description: 在添加、刪除、執(zhí)行監(jiān)聽(tīng)器的時(shí)候都要注意同步問(wèn)題 * 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() { } //注冊(cè)監(jiān)聽(tīng)器,如果這里沒(méi)有使用Vector而是使用ArrayList那么要注意同步問(wèn)題 public void addDemoListener(DemoListener dl) { repository.addElement(dl);//這步要注意同步問(wèn)題 } //如果這里沒(méi)有使用Vector而是使用ArrayList那么要注意同步問(wèn)題 public void notifyDemoEvent(DemoEvent event) { Enumeration enum = repository.elements();//這步要注意同步問(wèn)題 while(enum.hasMoreElements()) { dl = (DemoListener)enum.nextElement(); dl.demoEvent(event); } } //刪除監(jiān)聽(tīng)器,如果這里沒(méi)有使用Vector而是使用ArrayList那么要注意同步問(wèn)題 public void removeDemoListener(DemoListener dl) { repository.remove(dl);//這步要注意同步問(wèn)題 } /** * 設(shè)置屬性 * @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; //如果改變則執(zhí)行事件 if(bool) notifyDemoEvent(new DemoEvent(this,sName)); } public String getName() { return sName; } }
package demo; import java.lang.Thread; /** * Title: 測(cè)試類 * Description: 測(cè)試了由于改變屬性而引起的事件發(fā)生 * 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("添加監(jiān)聽(tīng)器完畢"); try { Thread.sleep(3000); //改變屬性,觸發(fā)事件 ds.setName("改變屬性,觸發(fā)事件"); } catch (InterruptedException ex) { ex.printStackTrace(); } ds.addDemoListener(this); System.out.println("添加監(jiān)聽(tīng)器完畢2"); try { Thread.sleep(3000); //改變屬性,觸發(fā)事件 ds.setName("改變屬性,觸發(fā)事件2"); } catch (InterruptedException ex) { ex.printStackTrace(); } ds.removeDemoListener(this); System.out.println("添加監(jiān)聽(tīng)器完畢3"); try { Thread.sleep(3000); //改變屬性,觸發(fā)事件 ds.setName("改變屬性,觸發(fā)事件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(); } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Java多線程+鎖機(jī)制實(shí)現(xiàn)簡(jiǎn)單模擬搶票的項(xiàng)目實(shí)踐
鎖是一種同步機(jī)制,用于控制對(duì)共享資源的訪問(wèn),在線程獲取到鎖對(duì)象后,可以執(zhí)行搶票操作,本文主要介紹了Java多線程+鎖機(jī)制實(shí)現(xiàn)簡(jiǎn)單模擬搶票的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02Javaweb項(xiàng)目session超時(shí)解決方案
這篇文章主要介紹了Javaweb項(xiàng)目session超時(shí)解決方案,關(guān)于解決方案分類比較明確,內(nèi)容詳細(xì),需要的朋友可以參考下。2017-09-09SpringBoot?SpringSecurity?詳細(xì)介紹(基于內(nèi)存的驗(yàn)證)
這篇文章主要介紹了SpringBoot?SpringSecurity?介紹(基于內(nèi)存的驗(yàn)證),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04