---
title: "JavaBean 学习一 "
author: "caohong286"
date: "2012-09-19T00:09:23Z"
tags: ["Introduction to Java", "Java", "Prentice Hall", "javabean"]
categories: ["编程语言"]
slug: "javabean"
draft: false
lastmod: "2012-09-19T00:09:23Z"
id: 1680927
url: "https://caohong286.iteye.com/blog/1680927"
---

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组件模型，满足下列要求：

- A bean must be a public class.

<!--THE END-->

- A bean must have a public no-arg constructor, though it can have other constructors if needed. For example, a bean named MyBean must either have a constructor with the signature

        public MyBean();

        or have no constructor if its superclass has a no-arg constructor.

- A bean must implement the java.io.Serializable interface to ensure a persistent state.

<!--THE END-->

- A bean usually has properties with correctly constructed public accessor (get) methods and mutator (set) methods that enable the properties to be seen and updated visually by a builder tool.

<!--THE END-->

- A bean may have events with correctly constructed public registration and deregistration methods that enable it to add and remove listeners. If the bean plays a role as the source of events, it must provide registration methods for registering listeners. For example, you can register a listener for ActionEvent using the addActionListener method of a JButton bean.

<!--THE END-->

- 一个bean必须是个公共类

<!--THE END-->

- 一个bean必须有一个无参数的构造函数，虽然如果需要它也能有其他的构造函数。例如，一个名叫MyBean的bean，必须有一个构造函数如

         public MyBean();

- 一个bean必须执行 java.io.Serializable接口确保持久状态。

<!--THE END-->

- 一个bean通常有属性带有正确构造的访问器（get）方法和设置器（set）方法，使得属性能被构建工具视觉化的可见和更新。

<!--THE END-->

- 一个bean可以有事件，具有正确构建的公有注册和注销方法，使得它能够增加和去除监听器。如果bean扮演事件源的角色，它必须对注册的监听器提供注册方法。例如你可使用Jbutton bean的addActionListener方法对ActionEvent注册一个监听器。

 

    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.

![](http://dl.iteye.com/upload/attachment/0074/0194/ac23bcd0-e34e-3ba0-99b4-5a51cdcdf412.jpg)