32.1 Introduction
Every Java user interface class is a JavaBeans component. Understanding JavaBeans will help
you to learn GUI components. In Chapter 16, “Event-Driven Programming,” you learned how
to handle events fired from source components such as JButton, JTextField, JRadio-
Button, and JComboBox. In this chapter, you will learn how to create custom events and de-
velop your own source components that can fire events. By developing your own events and
source components, you will gain a better understanding of the Java event model and GUI
components.
每个Java用户接口类是一个JavaBeans组件。理解JavaBeans将有助于你学习GUI组件。在第16章,“事件-驱动 编程”你学习了如何处理源组件触发的事件,如JButton,JTextField,JRadioButton和JcomboBox。本章,你将徐汇如何创建自定义的事件和开发你自己的能触发事件的源组件。通过开发你自己的事件和源组件,你将更好的理解Java事件模型和GUI组件。
32.2 JavaBeans
JavaBeans is a software component architecture that extends the power of the Java language
by enabling well-formed objects to be manipulated visually at design time in a pure Java
builder tool, such as NetBeans and Eclipse. Such well-formed objects are referred to as
JavaBeans or simply beans. The classes that define the beans, referred to as JavaBeans com-
ponents or bean components conform to the JavaBeans component model with the following
requirements:
JavaBeans是一个软件组件架构,继承了Java语言的强大功能,通过应用纯Java构建工具,如NetBeans和Eclipse,使得格式完整的对象能在设计时视觉化的操作。这种格式完整的对象称之为JavaBeans或者简单Bean。定义这些Bean的类,称之为JavaBean 组件或者bean组件,它们遵从于JavaBean组件模型,满足下列要求:
public MyBean();
or have no constructor if its superclass has a no-arg constructor.
public MyBean();
The first three requirements must be observed, and therefore are referred to as minimum Java-
Beans component requirements. The last two requirements depend on implementations. It is
possible to write a bean component without get/set methods and event registration/deregistration
methods.
前三个条件是必须遵从的,因此被称为最小Javabean组件要求。后两个要求取决于具体执行。有可能写一个bean组件没有get/set方法和事件注册/注销方法。
A JavaBeans component is a special kind of Java class. The relationship between JavaBeans components and Java classes is illustrated in Figure 32.1.
一个JavaBean组件是一个特殊种类的Java类。JavaBean组件和Java类的关系如图32.1所示。
Every GUI class is a JavaBeans component, because
1. it is a public class,
2. it has a public no-arg constructor, and
3. It is an extension of java.awt.Component, which implements java.io.Serializable.
每个GUI类是一个JavaBean组件,因为
1.它是一个公有类。
2.它有个公有的无参数的构造器。
3.它继承至java.awt.Component,它执行java.io.Serializable.