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
分享到:
相关推荐
使用Oracle数据库+Eclipse+MyEclipse,JSP+Servlet+JavaBean的基本组合,适合刚刚学习JSP的朋友参考一下~
由于这是本人学习JAVA的第一个项目,是在边学边做的情况下完成的。所以,有什么不好的地方请大家多多指教。 相信现在很多人都已经在使用Struts框架了。但是我也相信,还有那么一部分的人还是在抱怨着JAVA是多么难。 ...
A. 本留言本采用jsp+javabean+servlet+mysql B. 本系统运行环境: ...6、 此留言本实现了MVC模式,完全是通过servlet控制数据,而且使用了连接池,一些基本的类也是完全提供,对学习jsp的MVC模式有很大的帮助。
基于JSP+JavaBean+Servlet的课程管理系统的总体设计目标是为教师和学生提供一个课程管理平台。学生可以通过该系统快速地选择自己喜欢的课程,并且可以查看教师已经给出的某门课程的成绩,此外学生还可以更改自己的...
基于javabean+servlet 简单的书籍管理系统源码+项目说明.zip基于javabean+servlet 简单的书籍管理系统源码+项目说明.zip基于javabean+servlet 简单的书籍管理系统源码+项目说明.zip基于javabean+servlet 简单的书籍...
基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写...
基于JSP+Servlet+JavaBean的超市进销存管理系统源码.zip基于JSP+Servlet+JavaBean的超市进销存管理系统源码.zip基于JSP+Servlet+JavaBean的超市进销存管理系统源码.zip基于JSP+Servlet+JavaBean的超市进销存管理系统...
基于JSP+JavaBean+Servlet做的简单的学生选课系统源码.zip基于JSP+JavaBean+Servlet做的简单的学生选课系统源码.zip基于JSP+JavaBean+Servlet做的简单的学生选课系统源码.zip基于JSP+JavaBean+Servlet做的简单的学生...
基于jsp+servlet+javaBean的图书管理系统源码+数据库+项目说明.zip基于jsp+servlet+javaBean的图书管理系统源码+数据库+项目说明.zip基于jsp+servlet+javaBean的图书管理系统源码+数据库+项目说明.zip基于jsp+...
网上问卷调查系统 使用myeclipse编写 javaBean+JSP+Servlet.zip 1、该资源内项目代码经过严格调试,下载即用确保可以运行! 2、该资源适合计算机相关专业(如计科、人工智能、大数据、数学、电子信息等)正在做课程...
本项目主要为了巩固J2EE开发模型技术,学习选择J2EE架构技术,了解开发工具软件及其版本。该项目涉及到了软件架构中的三层架构模式MVC架构模式,该架构包括控制层、持久层和逻辑显示层三个层次。
三、数据表含义 表名 介绍 selectinfo 选择题表,包括题目、答案、难度、对应章节、ABCD四个选项的内容 selectdiff 选择题的难度,简单、容易、困难、复杂 selectchp 选择题对应的章节,及所占比重 paper 试卷,及...
功能完整的小区物业管理系统,包含业主和管理员,适合学习初学javaweb开发学习者使用!使用三层架构模式,其实包含java操作excel表格等功能!欢迎下载学习!
1:中小型物流信息管理系统 2:jsp实现,代码简单易读,适合初学者学习 3:系统可运行,内含数据库
三. 项目介绍: 1. 基于 Servlet+Jsp+JavaBean+Java实现的课程管理系统, 可用于毕业设计。代码结构清晰,可学习或者重新定制开发。 2. 项目具有详细的开发文档说明,环境配置说明,亲测可运行。 3. 系统说明 3.1 ...
本资源是javaBean+servlet+jsp中的常见异常,如果亲们在学习这一部分是有类似的问题,可见参考这个,里面有解决方案,记得评论哦
2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于...
1、该资源内项目代码都经过测试...3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 -------- 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
三. 项目介绍: 1. 基于 JSP+JAVABEAN+MYSQL的新闻发布系统 可用于毕业设计。代码结构清晰,可学习或者重新定制开发。 2. 项目具有详细的开发文档说明,环境配置说明,亲测可运行。 3. 系统说明 3.1 系统介绍 本系统...
基于JSP+Servlet+JavaBean+JDBC的学生选课系统,包括管理课程,学生选课和学生管理.zip 1、该资源内项目代码经过严格调试,下载即用确保可以运行! 2、该资源适合计算机相关专业(如计科、人工智能、大数据、数学、...