`
阅读更多

 

32.4 Java Event Model Review
A bean may communicate with other beans. The Java event delegation model provides the
foundation for beans to send, receive, and handle events. Let us review the Java event model
that was introduced in Chapter 16, “Event-Driven Programming.” The Java event model consists of the following three types of elements, as shown in Figure 16.3:

  • The event object
  • The source object
  • The event listener object

一个bean可以同其他的bean交流。Java 时间委托模型提供bean发送,接收和处理事件的基础,让我们回顾16章“事件-驱动编程”中介绍的Java事件模型。Java时间模型由下列三种元素组成,如图16.3所示

  • 事件对象
  • 源对象
  • 事件监听对象

An event is a signal to the program that something has happened. It can be triggered by external user actions, such as mouse movements, mouse button clicks, and keystrokes, or by the operating system, such as a timer. An event object contains the information that describes the event. A source object is where the event originates. When an event occurs on a source object, an event object is created. An object interested in the event handles the event. Such an object is called a listener. Not all objects can handle events. To become a listener, an object must be registered as a listener by the source object. The source object maintains a list of liteners and notifies all the registered listeners by invoking the event-handling method implemented on the listener object. The handlers are defined in the event listener interface. Each event class has a corresponding event listener interface. The Java event model is referred to as a delegation-based model, because the source object delegates the event to the listeners for processing.

一个事件对程序而言是发生某事的一个信号。它能被外部用户动作触发,如鼠标移动,鼠标按钮点击,击键盘,或者被操作系统,如一个定时器timer。事件对象包含描述事件的信息。产生事件的对象是源对象。当事件发生在源对象之上时,事件对象产生。对某事件感兴趣的对象处理该事件。这样的对象称为监听器。不是所有对象能处理事件。要成为一个监听器,对象必须被源对象注册成监听对象。源对象包含一监听列表且通过调用在监听器上执行的事件处理方法通知所有注册的监听器。每个事件类有对一个对应的事件监听接口。 Java事件模型称之为基于委托的模型,因为源对象委托事件给监听器处理。

 

32.4.1 Event Classes and Event Listener Interfaces
     An event object is created using an event class, such as ActionEvent , MouseEvent , and
ItemEvent , as shown in Figure 16.2. All the event classes extend java.util.Event Object . The event class contains whatever data values and methods are pertinent to the particular event type. For example, the Key Event class describes the data values related to a key event and contains the methods, such as getKeyChar(), for retrieving the key associated with the event.

    一个事件对象有一个事件类创建,如ActionEvent , MouseEvent ,和ItemEvent,如图16.2,所有的事件类继承于 java.util.Event对象。事件类包含特定事件类型相关的数据值和方法。例如,Key事件类描述相关key事件的数据值和包含方法如getKeyChar(),用于检索与key关联的事件。


     Every event class is associated with an event listener interface that defines one or more
methods referred to as handlers. An event listener interface is a subinterface of java.util.EventListener . The handlers are implemented by the listener components. The source component invokes the listeners’ handlers when an event is detected.

    每个事件类关联一个事件监听器接口,定义一个或多个称为处理器的方法。一个事件监听接口是一个java.util.EventListener的子接口。处理器由监听器组件执行。当侦测到某事件发生时源组件调用监听器的处理器
     Since an event class and its listener interface are coexistent, they are often referred to as an
event set or event pair. The event listener interface must be named as XListener for the
XEvent. For example, the listener interface for ActionEvent is ActionListener . The parameter list of a handler always consists of an argument of the event class type. Table 16.2 lists some commonly used events and their listener interfaces. Figure 32.2 shows the pair of ActionEvent and ActionListener.

    由于一个事件类和它的监听器接口是共生的,它们经常称为一事件集或事件对。对XEvent而言,事件监听器接口必须命名为XListener。 例如,ActionEvent 监听接口是ActionListener。一个处理器的参数列表总会包含一个事件类类型参数。表16.2列出了一些常见使用的处理器和他们的监听器接口。图32.2显示了ActionEvent和ActionListener。

 

32.4.2 Source Components
    The component on which an event is generated is referred to as an event source. Every Java GUI component is an event source for one or more events. For example, JButton is an event source for ActionEvent . A JButton object fires a java.awt.event.ActionEvent when it is clicked. JComboBox is an event source for ActionEvent and ItemEvent . A JComboBox object fires a java.awt.event.ActionEvent and a java.awt.event.ItemEvent when a new item is selected in the combo box.

    在其上发生事件的组件称之为事件源。每个Java GUI组件是一个事件源对应一个或多个事件。例如,JButton 对ActionEvnt而言是一个事件源。JButton 被点击时对象触发一个java.awt.event.ActionEvent。JComboBox 对ActionEvent 和 ItemEvent来说是一个事件源。JComboBox对象触发java.awt.event.ActionEvent和java.awt.event.ItemEvent,当一个新的选项选入combo选择框中。
    The source component contains the code that detects an external or internal action that triggers the event. Upon detecting the action, the source should fire an event to the listeners by invoking the event handler defined by the listeners. The source component must also contain methods for registering and deregistering listeners, as shown in Figure 32.3

    源组件包含代码侦测到一个外部或内部触发事件的动作。根据触发动作,源通过调用由监听器定义的事件处理器,触发事件给监听器。源组件也必须包含方法注册和注销监听器,如图32.3所示。

32.4.3 Listener Components
    A listener component for an event must implement the event listener interface. The object of
the listener component cannot receive event notifications from a source component unless the
object is registered as a listener of the source.

    一事件的监听器组件必须执行事件监听接口。除非对象被注册成源的监听器,否则监听组件对象不能接收来至源组件的事件通知。
    A listener component may implement any number of listener interfaces to listen to several
types of events. A source component may register many listeners. A source component may
register itself as a listener.

   监听组件可以执行任意个监听接口来监听几种类型的事件。源组件可以注册多个监听器。源组件可以注册它自己成一个监听器。
    Listing 32.1 gives an example that creates a source object (line 8) and a listener object (line
14), and registers the listener with the source object (line 17). Figure 32.4 highlights the relationship between the source and the listener. The listener is registered with the source by invoking the addActionListener method. Once the button is clicked, an ActionEvent is generated by the source. The source object then notifies the listener by invoking the listener’s actionPerformed method.

   代码片段32.1给出了一个创建一个源对象的例子(第8行)和一个监听对象(第14行),以及注册源对象的监听器(17行)。图32.4高亮出源和监听的关系。监听器由源调用addActionListener 方法注册。当button点击,源产生一个ActionEvent 。接着源对象调用监听器的actionPerformed 方法通知监听器。

 

import javax.swing.*;
import java.awt.event.*;

public classTestSourceListener {
 public static void main(String[] args) {
    JFrame frame = new JFrame("TestSourceListener");
    // Create a source object
    JButton jbt = new JButton("OK");
    frame.add(jbt);
    frame.setSize(200,200);
    frame.setVisible(true);

    // Create listeners
    MyListener listener = newMyListener();

     // Register listeners
     jbt.addActionListener(listener);
   }
 }

/** MyListener class */
  class MyListener implements ActionListener {
      public void ActionPerformed(ActionEvent e){
           System.out.println("I will process it!");
    }
 }
 

 

 

 

  • 大小: 66.7 KB
  • 大小: 44.9 KB
  • 大小: 99.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics