Home Java javaTutorial Code example for custom listening events

Code example for custom listening events

May 06, 2017 pm 12:52 PM

This article mainly introduces relevant information about JAVA user-defined event monitoring example code. Friends who need it can refer to

JAVA user-defined event monitoring example code

Many introductions to user-defined events do not have examples, or the examples are incomplete. A complete example is written below, and comments are written for reference. The complete source code of the example is as follows:

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;
  }
}
Copy after login
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);
}
Copy after login
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;
  }
}
Copy after login
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();
  }
}
Copy after login

[Related Recommended】

1. Java Free Video Tutorial

2. Alibaba Java Development Manual

3. Comprehensive analysis of Java annotations

The above is the detailed content of Code example for custom listening events. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles