heifei():感谢您的关心,刚才的帖子有点错,更正如下:
/********************
    /*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);
}
另外,根据书上说的好象不是您说的那样:
An event source is a component or object that generates events. A
common example of an event source is a GUI button, although not
all event sources need to be GUI components. For example, an
internal timer object could be an event source that creates an event
object once every second.
Event sources have three main responsibilities:
 Provide methods that allow other methods to add and remove event listeners.
 Maintain a list of interested event listeners.
 Contain logic to create and deliver event objects of the
appropriate type (class) to all interested event listeners.
can literally come from any object of any type. It
does not have to subclass any specific class or implement any
specific interface. Additionally, an object can act as the source of
several different event types, provided that it fulfills the
responsibilities listed previously for each type.

解决方案 »

  1.   

    对不起,是我把操作系统事件与java的awt事件说混淆了,下面是来自sun新闻组对how events are passed?的描述:
    When the user clicks the mouse, types on the keyboard, drags and drops, or does any of a few other 
    things, the operating system produces an event(操作系统事件). This event is passed to Java, annd the Java runtime 
    tries to figure out which component the event(awt事件) was intended for. 
      
    The Java runtime then passes that event to the handleEvent(Event e) method of soome 
    Component. The Component's handleEvent() method contains a big if-else statementt to look 
    at the type of event and respond appropriately. What the handleEvent() method dooes depends 
    on the type of component. Generally some events are ignored and other events aree passed to 
    methods that know how to respond to those events.
    但是Button, TextField, Checkbox等组件好像没有mouse事件,其实同样产生操作系统事件,只不过awt把有效地鼠标事件统一封装为actionEvent而已。
      

  2.   

    我的理解是:组件外界因素产生的事件,是直接派发给由事件源注册到ListenerList中的监听器,现在我想知道是: 1)public void addFooListener(FooListener l){
               //Code to add listener to the list of listeners.
              }
    该如何实现,才能在事件发生时触发
          private void notifyFooListeners(){...}
    我注意到它是private,应该不能从外部调用。
       即:进入notifyFooListeners()的进入点在哪里?也就是事件发生后由什么来调用notifyFooListeners()?它怎么知道该调用的是notifyFooListeners()方法?   2)具体该如何注册监听器,才能在事件发生时不经过事件源,直接触发ListenerList中的监听器?
      

  3.   

    整个过程是这样:事件产生-插入队列-队列线程调用getNextEvent()获得某事件-....-调用事件的getSource()获得事件的源组件-调用源组件的dispatchEvent()方法-调用源组件的processEvent()方法-根据Event的类型xxx调用processxxxEvent()-if(xxxListener!=null){执行xxxListener接口实现的方法}
    1,对于你的代码我也看不明白,组件不应该自己notify listener的,除非内部事件,能不能说一下你这段代码的出处?
    2,你的意思是:事件产生->if(xxxListener!=null){执行xxxListener接口实现的方法}也就是绕过中间的一大段awt标准过程?这不太容易,对外部事件首先产生操作系统事件-通过虚拟机-进入以上所说的awt事件机制.要实现有两种可能:
    a,在awt事件机制内:随时监控事件队列,当队列发生改变得时候用队列尾部的新事件的getSource()方法查看是否是你感兴趣的组件的事件,决定是否触发Listener;不过这有个问题:你的监控线程与队列内部线程需要同步,因为都存在对对列的remove()方法,所以两个线程交替执行时有可能因为监控线程的阻塞而漏掉刚好在此时插入的事件,让他进入了awt事件机制.
    b,在jvm传递到awt这个过程中拦截:这是我的猜想,我不知道sun的虚拟机是否能实现,不过你如果用自己的jvm/网上也有好多带源码的免费虚拟机应该能实现.
    c,也许有高手还有更好的方法.
      

  4.   

    1)以上的代码来自Sun Certified Programmer for Java 2 Study Guide (Exam 310-025) (2)
      

  5.   

    wunglee兄说得太过了,我也没有什么内幕代码,我想学习java基础的最好途径是看书结合,jdk的类库代码,学习delphi就看vcl源代码...都是一个道理,再大而全的书也不可能面面俱到,很多时候得靠自己,我说的也只是主要流程,你可以自己看
    java.awt.Event===awt事件
    java.awt.Component===awt组件
    java.awt.Toolkit==abstract window toolkit
    java.awt.EvenQueue== 事件队列
    java.awt.EventDisptachThread==事件派发线程
    主要就是这几个类,看了你就能对awt事件机制有个大概的了解。