JavaBeans开发:高级特性与定制化指南
立即解锁
发布时间: 2025-08-18 00:08:29 阅读量: 2 订阅数: 11 


深入理解Java高级特性与应用开发
### JavaBeans开发:高级特性与定制化指南
在Java开发中,JavaBeans为组件化编程提供了强大的支持。然而,仅依赖标准命名模式来开发JavaBeans存在一定的局限性。下面将深入探讨如何超越命名模式,利用JavaBeans的高级特性进行开发。
#### 超越命名模式
使用标准命名约定时,构建工具可以通过反射来确定Bean的属性、事件和方法。但当Bean变得复杂时,命名模式和反射无法揭示其所有特性,而且将英语命名模式作为所有GUI构建器的基础也不利于为国际客户提供支持。此外,有些get/set对可能不希望作为Bean属性暴露。
JavaBeans规范提供了一种更灵活、强大的机制,即使用实现`BeanInfo`接口的对象来存储Bean信息。虽然理论上简单,但实践中可能较为繁琐。需要注意的是,要将`BeanInfo`对象与Bean关联,需遵循命名模式,即在Bean名称后添加`BeanInfo`,且该类必须与Bean处于同一包中。
通常,不会从头开始编写实现`BeanInfo`接口的类,而是使用`SimpleBeanInfo`类,它为`BeanInfo`接口的所有方法提供了空实现(返回`null`),只需重写需要更改的方法。该类还包含`loadImage`方法,可用于为Bean加载图像。
以下是`ChartBeanBeanInfo`类的示例:
```java
public class ChartBeanBeanInfo extends SimpleBeanInfo
```
#### 自定义图标
`BeanInfo`接口中的`getIcon`方法可让为Bean设置自定义图标,构建工具通常会在工具盒中显示Bean名称旁边的图标。`BeanInfo`接口有四个常量用于表示标准图标大小:
- `ICON_COLOR_16x16`
- `ICON_COLOR_32x32`
- `ICON_MONO_16x16`
- `ICON_MONO_32x32`
以下是使用`SimpleBeanInfo`类的`loadImage`方法添加图标的示例:
```java
public Image getIcon(int iconType)
{
String name = "";
if (iconType == BeanInfo.ICON_COLOR_16x16)
name = "COLOR_16x16";
else if (iconType == BeanInfo.ICON_COLOR_32x32)
name = "COLOR_32x32";
else if (iconType == BeanInfo.ICON_MONO_16x16)
name = "MONO_16x16";
else if (iconType == BeanInfo.ICON_MONO_32x32)
name = "MONO_32x32";
else return null;
return loadImage("ChartBean_" + name + ".gif");
}
```
#### FeatureDescriptor对象
`FeatureDescriptor`类及其子类是使用`BeanInfo`类高级特性的关键。`FeatureDescriptor`对象用于提供关于特性的信息,其常见子类包括:
- `BeanDescriptor`
- `EventSetDescriptor`
- `MethodDescriptor`
- `ParameterDescriptor`
- `PropertyDescriptor`(还有子类`IndexedPropertyDescriptor`)
创建描述符对象来描述每个成员,并将同一特性集的描述符收集到数组中,作为`BeanInfo`方法的返回值。例如,要关闭事件集的反射,可在Bean信息类中返回`EventSetDescriptor`对象数组:
```java
class MyBeanBeanInfo extends SimpleBeanInfo
{
public EventSetDescriptor[] getEventSetDescriptors()
{
...
}
...
}
```
以下是创建`EventSetDescriptor`对象的示例:
```java
EventSetDescriptor push = new EventSetDescriptor(beanClass,
"actionPerformed",
java.awt.event.ActionListener.class,
"actionPerformed");
EventSetDescriptor changed = new EventSetDescriptor(beanClass,
"propertyChange",
java.beans.PropertyChangeListener.class,
"propertyChange");
push.setDisplayName("button push");
changed.setDisplayName("bound property change");
```
由于所有`FeatureDescriptor`对象的构造函数可能会抛出`IntrospectionException`,因此需要在`try/catch`块中构建描述符数组:
```java
public EventSetDescriptor[] getEventSetDescriptors()
{
try
{
EventSetDescriptor push = new
EventSetDescriptor(beanClass,
"actionPerformed",
java.awt.event.ActionListener.class,
"actionPerformed");
EventSetDescriptor changed = new
EventSetDescriptor(beanClass,
"propertyChange",
java.beans.PropertyChangeListener.class,
"propertyChange");
push.setDisplayName("button push");
changed.setDisplayName("bound property change");
return new EventSetDescriptor[] { push, changed };
}
catch (IntrospectionException e)
{
throw new RuntimeException(e.toString());
}
}
```
当Bean的任何特性与标准命名模式不同时,需要:
1. 为该特性集中的所有特性创建特性描述符。
2. 在相应的`BeanInfo`方法中返回描述符数组。
#### JavaBeans相关接口和类的方法
| 接口/类 | 方法 | 描述 |
| ---- | ---- | ---- |
| `java.beans.BeanInfo` | `EventSetDescriptor[] getEventSetDescriptors()` | 返回事件集描述符数组 |
| | `MethodDescriptor[] getMethodDescriptors()` | 返回方法描述符数组 |
| | `PropertyDescriptor[] getPropertyDescriptors()` | 返回属性描述符数组 |
| | `Image getIcon(int iconType)` | 返回用于表示Bean的图像对象 |
| | `int getDefaultEventIndex()` | 返回默认事件的数组索引 |
| | `int getDefaultPropertyIndex()` | 返回默认属性的数组索引 |
| | `BeanInfo[] getAdditionalBeanInfo()` | 返回`BeanInfo`对象数组或`null` |
| `java.beans.SimpleBeanInfo` | `Image loadImage(String resourceName)` | 返回与资源关联的图像对象文件 |
| `java.beans.FeatureDescriptor` | `String getName()` | 返回成员在Bean代码中使用的名称 |
| | `void setName(String name)` | 设置特性的编程名称 |
| | `String getDisplayName()` | 返回特性的本地化显示名称 |
| | `void setDisplayName(String displayName)` | 设置特性的本地化显示名称 |
| | `String getShortDescription()` | 返回构建工具可用于提供特性简短描述的本地化字符串 |
| | `void setShortDescription(String text)` | 设置描述特性的简短字符串 |
| | `void setValue(String attributeName, Object value)` | 将命名属性与该特性关联 |
| | `Object getValue(String attributeName)` | 获取具有给定名称的特性的值 |
| | `Enumeration attributeNames()` | 返回包含通过`setValue`注册的任何属性名称的枚举对象 |
| | `void setExpert(boolean b)` | 设置专家标志 |
| | `boolean isExpert()` | 判断该特性是否为专家使用 |
| | `void setHidden(boolean b)` | 标记特性仅由构建工具使用 |
| | `boolean isHidden()` | 判断用户是否不应看到该特性 |
| `java.beans.EventSetDescriptor` | `EventSetDescriptor(Class sourceClass, String eventSetName, Class listener, String listenerMethod)` | 构造`EventSetDescriptor`对象 |
| | `EventSetDescriptor(Class sourceClass, String eventSetName, Class listener, String[] listenerMethods, String addListenerMethod, String removeListenerMethod)` | 构造具有多个监听器方法和自定义添加/删除监听器方法的`EventSetDescriptor`对象 |
| | `Method getAddListenerMethod()` | 返回用于注册监听器的方法 |
| | `Method getRemoveListenerMethod()` | 返回用于移除已注册监听器的方法 |
| | `Method[] getListenerMethods()` | 返回监听器接口中触发的方法数组 |
| | `Metho
0
0
复制全文
相关推荐










