当前位置: 技术问答>java相关
怎样自定义事件?
来源: 互联网 发布时间:2015-01-06
本文导语: 要注意哪些? | /////你首先作三件事: ////1) Create an event object ////2) Create and maintain a listener list in an event source ////3) Create event listeners public class MyEvent extends java.util.EventObject { pri...
要注意哪些?
|
/////你首先作三件事:
////1) Create an event object
////2) Create and maintain a listener list in an event source
////3) Create event listeners
public class MyEvent extends java.util.EventObject {
private String action;
public MyEvent(Object source, String action) {
super(source);
this.action = action;
}
public String getAction() {
return action;
}
}
////创建object列表,知道单前事件在做什么,事件监听器的工作有:
1) add listeners
2) remove listeners
3) notifiy listeners
import java.util.*;
public class MyEventListenerList {
Vector list = new Vector();
public void addMyEventListener(MyEventListener l) {
//add the listner to the list
list.add(l);
}
public void removeMyEventListener(MyEventListener l) {
//remove the listener from the list
list.remove(l);
}
public void notifyMyEventListeners(MyEvent event) {
// iterate through each listener and call the
//handleMyEvent method for the listener
ListIterator i = list.listIterator();
while(i.hasNext()) {
MyEventListener l = (MyEventListener) i.next();
l.handleMyEvent(event);
}
}
}
////为监听器创建接口
public interface MyEventListener extends java.util.EventListener
{
public void handleMyEvent(MyEvent event);
}
/////在你的对象中创建事件监听器
public class TheListener implements MyEventListener {
public void handleMyEvent(MyEventObject event) {
System.out.println(event.getAction());
}
}
////创建监听对象
public class TheSource {
//Here's our list of MyEventObject listners
MyEventListenerList list = new MyEventListenerList();
public TheSource() {
//create a couple of listeners and add them to the list
TheListener theListener1 = new TheListener();
TheListener theListener2 = new TheListener();
list.addMyEventListener(theListener1);
list.addMyEventListener(theListener2);
list.notifyMyEventListeners(new MyEvent(this, "first"));
// remove one listener and send another event
list.removeMyEventListener(theListener2);
list.notifyMyEventListeners(new MyEvent(this, "second"));
}
public static void main(String args[]){
new TheSource();
}
}
////1) Create an event object
////2) Create and maintain a listener list in an event source
////3) Create event listeners
public class MyEvent extends java.util.EventObject {
private String action;
public MyEvent(Object source, String action) {
super(source);
this.action = action;
}
public String getAction() {
return action;
}
}
////创建object列表,知道单前事件在做什么,事件监听器的工作有:
1) add listeners
2) remove listeners
3) notifiy listeners
import java.util.*;
public class MyEventListenerList {
Vector list = new Vector();
public void addMyEventListener(MyEventListener l) {
//add the listner to the list
list.add(l);
}
public void removeMyEventListener(MyEventListener l) {
//remove the listener from the list
list.remove(l);
}
public void notifyMyEventListeners(MyEvent event) {
// iterate through each listener and call the
//handleMyEvent method for the listener
ListIterator i = list.listIterator();
while(i.hasNext()) {
MyEventListener l = (MyEventListener) i.next();
l.handleMyEvent(event);
}
}
}
////为监听器创建接口
public interface MyEventListener extends java.util.EventListener
{
public void handleMyEvent(MyEvent event);
}
/////在你的对象中创建事件监听器
public class TheListener implements MyEventListener {
public void handleMyEvent(MyEventObject event) {
System.out.println(event.getAction());
}
}
////创建监听对象
public class TheSource {
//Here's our list of MyEventObject listners
MyEventListenerList list = new MyEventListenerList();
public TheSource() {
//create a couple of listeners and add them to the list
TheListener theListener1 = new TheListener();
TheListener theListener2 = new TheListener();
list.addMyEventListener(theListener1);
list.addMyEventListener(theListener2);
list.notifyMyEventListeners(new MyEvent(this, "first"));
// remove one listener and send another event
list.removeMyEventListener(theListener2);
list.notifyMyEventListeners(new MyEvent(this, "second"));
}
public static void main(String args[]){
new TheSource();
}
}