java 自定义事件

package  com.te.event.other;

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  com.te.event.other;

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  com.te.event.other;

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 enu 
=  repository.elements(); // 这步要注意同步问题
         while  (enu.hasMoreElements()) {
            dl 
=  (DemoListener) enu.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  com.te.event.other;

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

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值