# java 事件(java Event)
# java.util.Eventobject,java.util.EventListener
# victor
# 2016.06.16
Java 事件处理机制
java中的事件机制的参与者有3种角色:
1. event object:事件状态对象,是一类事件的实例。
伴随着事件的发生,相应的状态通常都封装在事件状态对象中,该对象必须继承自java.util.EventObject。事件状态对象作为单参传递给应响应该事件的监听者方法中。
2. event source:具体的事件源,事件的生产者。
发出某种特定事件的事件源的标识是:遵从规定的设计格式为事件监听者定义注册方法,并接受对指定事件监听者接口实例的引用。
3. event listener:事件监听者,事件的消费者。
具体的对监听的事件类,当它监听到event object产生的时候,它就调用相应的方法,进行处理。
<Note>
public interface EventListener:所有事件侦听器接口必须扩展的标记接口。
public class EventObject extends Object implements Serializable。所有事件状态对象都将从其派生的根类。 所有 Event 在构造时都引用了对象 "source",在逻辑上认为该对象是最初发生有关 Event 的对象。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
运行结果:
Console>> Console is started. lights>> lights are changing. Light 0>> is off Light 1>> is off Light 2>> is on Light 3>> is off Light 4>> is on Light 5>> is on Light 6>> is off Light 7>> is on Light 8>> is on Light 9>> is on lights>> lights are changing. Light 0>> is on Light 1>> is on Light 2>> is on Light 3>> is on Light 4>> is off Light 5>> is on Light 6>> is on Light 7>> is off Light 8>> is on Light 9>> is off
OUTPUT
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
JAVA CODE:
1. 定义事件对象
package event; import java.util.EventObject; public class LightEvent extends EventObject { private static final long serialVersionUID = -1256654879784096588L; private boolean lightstate = false; private String lightname; public String getLightname() { return lightname; } public boolean getLightstate() { return lightstate; } public LightEvent(Object source,String name,boolean state) { super(source); this.lightstate = state; this.lightname = name; } }
Event Class
2. 定义事件监听接口和具体的监听者类
package event; import java.util.EventListener; public interface lightlistener extends EventListener { public abstract void listenlight (LightEvent event); } package event; public class lightlistener1 implements lightlistener{ public void listenlight (LightEvent event){ if(event.getLightstate()){ System.out.println(""+event.getLightname()+">> is on"); } else{ System.out.println(""+event.getLightname()+">> is off"); } }; }
Listener interface & Concrete listener
3. 定义事件源和注册类
//register package event; import java.util.*; public class listenerregister { public List<EventListener> listenerlist = new ArrayList<EventListener>(); public void addListener(EventListener listener) { listenerlist.add(listener); } public void removeListener(EventListener listener) { listenerlist.remove(listener); } public void notifylisteners(LightEvent event){ for(EventListener tmplistener : listenerlist){ ((lightlistener)tmplistener).listenlight(event); } } } // EventSource package event; import java.util.*; public class Console { private listenerregister register = new listenerregister(); private boolean[] lights = new boolean[10] ; private Random randomer = new Random(); public void setLights(boolean light, int index) { this.lights[index] = light; String name = "Light "+ index; LightEvent event = new LightEvent(this,name,light); notifylisteners(event); } public void notifylisteners(LightEvent event){ register.notifylisteners(event); } public void addListener(EventListener listener) { register.listenerlist.add(listener); } public void removeListener(EventListener listener) { register.listenerlist.remove(listener); } public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub System.out.println("Console>> Console is started."); Console console = new Console(); console.addListener(new lightlistener1()); while(true){ System.out.println("lights>> lights are changing."); for(int i=0;i<10;i++){ console.setLights(console.randomer.nextBoolean(), i); } Thread.sleep(1000); } } }
EventSource & Register