java事件監聽

java事件機制包括三個部分:事件、事件監聽器、事件源。

 

1、事件。一般繼承自java.util.EventObject類,封裝了事件源對象及跟事件相關的信息。

com.javaedu.event.CusEvent類

Java代碼  收藏代碼
  1. package com.javaedu.event;  
  2.   
  3. import java.util.EventObject;  
  4.   
  5. /** 
  6.  * 事件類,用於封裝事件源及一些與事件相關的參數. 
  7.  * @author Eric 
  8.  */  
  9. public class CusEvent extends EventObject {  
  10.     private static final long serialVersionUID = 1L;  
  11.     private Object source;//事件源  
  12.       
  13.     public CusEvent(Object source){  
  14.         super(source);  
  15.         this.source = source;  
  16.     }  
  17.   
  18.     public Object getSource() {  
  19.         return source;  
  20.     }  
  21.   
  22.     public void setSource(Object source) {  
  23.         this.source = source;  
  24.     }  
  25. }  

 

2、事件監聽器。實現java.util.EventListener接口,註冊在事件源上,當事件源的屬性或狀態改變時,取得相應的監聽器調用其內部的回調方法。

com.javaedu.event.CusEventListener類

Java代碼  收藏代碼
  1. package com.javaedu.event;  
  2.   
  3. import java.util.EventListener;  
  4.   
  5. /** 
  6.  * 事件監聽器,實現java.util.EventListener接口。定義回調方法,將你想要做的事 
  7.  * 放到這個方法下,因爲事件源發生相應的事件時會調用這個方法。 
  8.  * @author Eric 
  9.  */  
  10. public class CusEventListener implements EventListener {  
  11.       
  12.     //事件發生後的回調方法  
  13.     public void fireCusEvent(CusEvent e){  
  14.         EventSourceObjecteObject = (EventSourceObject)e.getSource();  
  15.         System.out.println("My name has been changed!");  
  16.         System.out.println("I got a new name,named \""+eObject.getName()+"\"");    }  
  17. }  

 

3、事件源。事件發生的地方,由於事件源的某項屬性或狀態發生了改變(比如BUTTON被單擊、TEXTBOX的值發生改變等等)導致某項事件發生。換句話說就是生成了相應的事件對象。因爲事件監聽器要註冊在事件源上,所以事件源類中應該要有盛裝監聽器的容器(List,Set等等)。

 com.javaedu.event.EventSourceObject類

Java代碼  收藏代碼
  1. package com.javaedu.event;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Iterator;  
  5. import java.util.Set;  
  6.   
  7. /** 
  8.  * 事件源. 
  9.  * @author Eric 
  10.  */  
  11. public class EventSourceObject {  
  12.     private String name;  
  13.     //監聽器容器  
  14.     private Set<CusEventListener> listener;  
  15.     public EventSourceObject(){  
  16.         this.listener = new HashSet<CusEventListener>();  
  17.         this.name = "defaultname";  
  18.     }  
  19.     //給事件源註冊監聽器  
  20.     public void addCusListener(CusEventListener cel){  
  21.         this.listener.add(cel);  
  22.     }  
  23.     //當事件發生時,通知註冊在該事件源上的所有監聽器做出相應的反應(調用回調方法)  
  24.     protected void notifies(){  
  25.         CusEventListener cel = null;  
  26.         Iterator<CusEventListener> iterator = this.listener.iterator();  
  27.         while(iterator.hasNext()){  
  28.             cel = iterator.next();  
  29.             cel.fireCusEvent(new CusEvent(this));  
  30.         }  
  31.     }  
  32.     public String getName() {  
  33.         return name;  
  34.     }  
  35.     //模擬事件觸發器,當成員變量name的值發生變化時,觸發事件。  
  36.     public void setName(String name) {  
  37.         if(!this.name.equals(name)){  
  38.             this.name = name;  
  39.             notifies();  
  40.         }        
  41.     }  
  42. }  

下面是主方法類

 com.javaedu.event.MainTest類、

Java代碼  收藏代碼
  1. package com.javaedu.event;  
  2.   
  3. public class MainTest {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         EventSourceObject object = new EventSourceObject();  
  10.         //註冊監聽器  
  11.         object.addCusListener(new CusEventListener(){  
  12.             @Override  
  13.             public void fireCusEvent(CusEvent e) {  
  14.                 super.fireCusEvent(e);  
  15.             }  
  16.         });  
  17.         //觸發事件  
  18.         object.setName("eric");  
  19.     }  
  20. }  

 



轉自:http://ericliu1986.iteye.com/blog/629562

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章