关于事件派发的迷惑(100分)

  • 主题发起人 主题发起人 wunglee
  • 开始时间 开始时间
W

wunglee

Unregistered / Unconfirmed
GUEST, unregistred user!
/********************
/*file: FooEvent.java
/********************
package EventInterface;
public interface FooListener extends java.util.EventListener{
public void handleFooEvent(FooEvent evt);
}

/********************
/*file: FooEventGenerator.java
/********************
package EventSource;
import EventInterface.FooListener;
public class FooEventGenerator{
public void addFooListener(FooListener l){
//Code to add listener to the list of listeners.
}
public void removeFooListener(FooListener l){
// Code to remove listener from the list of listeners.
}
private void notifyFooListeners(){ //进入notifyFooListeners()的进入点在哪里?
//也就是事件发生后由什么来调用notifyFooListeners()?
//它怎么知道该调用的是notifyFooListeners()方法?
// Create a FooEvent with this as the source.
FooEvent fooEvent = new FooEvent(this);
// Deliver the event. //是不是事件源通过显式调用监听接口的方法来派发该事件?
}
}

/********************
/*file: FooEventGenerator.java
/********************
import EventInterface.FooListener
import EventSource.FooEventGenerator
public class FooTest implements FooListener{
// Implement the FooListener interface.
public void handleFooEvent(FooEvent evt){
System.out.println("Received FooEvent!");
}
}
FooEventGenerator eventSource = new FooEventGenerator();
FooTest listener = new FooTest();
eventSource.addFooListener(listener);
 
你的含有函数addListener(。。。)的类自然会知道这些。
因为这样的类肯定是继承了某些java的基类,他们知道该如何处理注册进来的listener类
 
有完整实现的例程吗?
 
以上的有误
/********************
/*file: FooEvent.java
/********************
package Eventpackage;
public class FooEvent extends java.util.EventObject{
public FooEvent(Object source){
super(source);
}
}

/********************
/*file: FooListener.java
/********************
package EventListener;
import Eventpackage.FooEvent;
public interface FooListener extends java.util.EventListener{
public void handleFooEvent(FooEvent evt);
}

/********************
/*file: FooEventGenerator.java
/********************
package EventSource;
import EventListener.FooListener;
import Eventpackage.FooEvent;
public class FooEventGenerator{
public void addFooListener(FooListener l){
//Code to add listener to the list of listeners.
}
public void removeFooListener(FooListener l){
// Code to remove listener from the list of listeners.
}
private void notifyFooListeners(){ //进入notifyFooListeners()的进入点在哪里?
//也就是事件发生后由什么来调用notifyFooListeners()?
//它怎么知道该调用的是notifyFooListeners()方法?
// Create a FooEvent with this as the source.
FooEvent fooEvent = new FooEvent(this);
// Deliver the event. //是不是事件源通过显式调用监听接口的方法来派发该事件?
}
}

/********************
/*file: FooTest.java
/********************
import EventListener.FooListener
import EventSource.FooEventGenerator
import Eventpackage.FooEvent;
public class FooTest implements FooListener{
// Implement the FooListener interface.
public void handleFooEvent(FooEvent evt){
System.out.println("Received FooEvent!");
}
}
FooEventGenerator eventSource = new FooEventGenerator();
FooTest listener = new FooTest();
eventSource.addFooListener(listener);
}
 
后退
顶部