服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - JAVA用户自定义事件监听实例代码

JAVA用户自定义事件监听实例代码

2020-09-13 12:19Java教程网 Java教程

这篇文章主要介绍了JAVA用户自定义事件监听实例代码的相关资料,需要的朋友可以参考下

JAVA用户自定义事件监听实例代码

很多介绍用户自定义事件都没有例子,或是例子不全,下面写了一个完整的例子,并写入了注释以便参考,完整的实例源代码如下:

?
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: 事件处理类,继承了事件基类
* 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;
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}
?
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: 使用事件的类
* 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;
  }
}
?
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: 测试类
* 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();
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/qq_26562641/article/details/50667496

延伸 · 阅读

精彩推荐