📄 0040.htm
字号:
<html>
<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>JavaBean用户化</strong></big></p>
<p><span class=smallFont>
JavaBean开发者可以给一个Bean添加用户化器(Customizer)、属性编辑器(PropertyEditor)和BeanInfo接口来描述一个Bean的内容,Bean的使用者可在构造环境中通过与Bean附带在一起的这些信息来用户化Bean的外观和应做的动作。一个Bean不必都有BeanCustomizer、PrpertyEditor和BeanInfo,根据实际情况,这些是可选的,当有些Bean较复杂时,就要提供这些信息,以Wizard的方式使Bean的使用者能够用户化一个Bean。有些简单的Bean可能这些信息都没有,则构造工具可使用自带的透视装置,透视出Bean的内容,并把信息显示到标准的属性表或事件表中供使用者用户化Bean,前几节提到的Bean的属性、方法和事件名要以一定的格式命名,主要的作用就是供开发工具对Bean进行透视。当然也是给程序员在手写程序中使用Bean提供方便,使他能观其名、知其意。 <br>
3.3.1用户化器接口(Customizer Interface) <br>
<br>
当一个Bean有了自己的用户化器时,在构造工具内就可展现出自己的属性表。在定义用户化器时必须要实现java.beans.Customizer接口。例如,下面是一个"按钮"Bean的用户化一器: <br>
<br>
<br>
public class OurButtonCustomizer extends Panel implements Customizer {<br>
... ...<br>
/*当实现象OurButtonCustomizer这样的常规属性表时,一定要在其中实现addProperChangeListener<br>
和removePropertyChangeListener,这样,构造工具可用这些功能代码为属性事件添加监听者。*/<br>
... ...<br>
private PropertyChangeSupport changes=new PropertyChangeSupport(this);<br>
public void addPropertyChangeListener(PropertyChangeListener l) {<br>
changes.addPropertyChangeListener(l);<br>
public void removePropertyChangeListener(PropertyChangeListener l) {<br>
changes.removePropertyChangeListener(l);<br>
}<br>
... ...<br>
<br>
3.3.2 属性编辑器接口(PropertyEditor Interface) <br>
<br>
一个JavaBean可提供PropertyEditor类,为指定的属性创建一个编辑器。这个类必须继承自java.beans.PropertyEditorSupport类。构造工具与手写代码的程序员不直接使用这个类,而是在下一小节的BeanInfo中实例化并调用这个类。例:<br>
<br>
<br>
public class MoleculeNameEditor extends java.beans.PropertyEditorSupport {<br>
public String[] getTags() {<br>
String resule[]={<br>
"HyaluronicAcid","Benzene","buckmisterfullerine",<br>
"cyclohexane","ethane","water"};<br>
return resule;}<br>
}<br>
<br>
上例中是为Tags属性创建了属性编辑器,在构造工具内,可从下拉表格中选择MoleculeName的属性应是"HyaluronicAid"或是"water"。 <br>
3.3.3BeanInfo接口 <br>
<br>
每个Bean类也可能有与之相关的BeanInfo类,在其中描述了这个Bean在构造工具内出现时的外观。BeanInfo中可定义属性、方法、事件,显示它们的名称,提供简单的帮助说明。 例如: <br>
<br>
<br>
public class MoleculeBeanInfo extends SimpleBeanInfo {<br>
public PropertyDescriptor[] getPropertyDescriptors() {<br>
try {<br>
PropertyDescriptor pd=new PropertyDescriptor("moleculeName",Molecule.class);<br>
/*通过pd引用了上一节的MoleculeNameEditor类,取得并返回moleculeName属性*/<br>
pd.setPropertyEditorClass(MoleculeNameEditor.class);<br>
PropertyDescriptor result[]={pd};<br>
return result;<br>
} catch(Exception ex) {<br>
System.err.println("MoleculeBeanInfo: unexpected exeption: "+ex);<br>
return null;<br>
}<br>
}<br>
}
</table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -