JAVA用戶(hù)自定義事件監(jiān)聽(tīng)實(shí)例代碼
很多介紹用戶(hù)自定義事件都沒(méi)有例子,或是例子不全,下面寫(xiě)了一個(gè)完整的例子,并寫(xiě)入了注釋以便參考,完整的實(shí)例源代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package demo; import Java.util.EventObject; /** * Title: 事件處理類(lèi),繼承了事件基類(lèi) * 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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package demo; import java.util.*; /** * Title: 使用事件的類(lèi) * Description: 該類(lèi)實(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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package demo; import java.lang.Thread; /** * Title: 測(cè)試類(lèi) * 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ì)本站的支持!
原文鏈接:http://blog.csdn.net/qq_26562641/article/details/50667496