今天学习了下JavaBean的属性,JavaBean都具有属性、事件和方法三大特性,JavaBean数学包括:Sample属性,Indexed属性、Bound属性和Constrained属性。创建简单的属性,如name属性呀,在初学一中有介绍,而且作为bena的属性是可以更改其内容的。
1、创建简单的属性编辑器
创建一个标准的javaBean,要先创建一个标准的java项目,再创建一个包名:mybeans
接着就是在包中,创建其他,一个java类
在这个java类中输入下面的代码
package mybeans;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.beans.*;
import java.io.Serializable;
/**
*
* @author Administrator
*/
public class FirstLabel extends Component implements Serializable {
public static final String PROP_TEXT = "text";
private String text = "MyLabel1";
private PropertyChangeSupport propertySupport;
public FirstLabel() {
propertySupport = new PropertyChangeSupport(this);
this.setPreferredSize(new Dimension(60, 30));
}
public String getText(){
return text;
}
public void setText(String value){
String OldValue = text;
text = value;
propertySupport.firePropertyChange(PROP_TEXT,OldValue,text);
}
public void addPropertyChangeListener(PropertyChangeListener listener){
propertySupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener){
propertySupport.removePropertyChangeListener(listener);
}
public void paint(Graphics g){
if(text != null && text != " "){
g.drawString(text,0,getHeight()-5);
}
}
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.beans.*;
import java.io.Serializable;
*
* @author Administrator
*/
public class MyShape extends Component implements Serializable {
private String shape = "Rectangle";
private PropertyChangeSupport propertySupport;
propertySupport = new PropertyChangeSupport(this);
this.setPreferredSize(new Dimension(50, 50));
}
return shape;
}
String oldValue = shape;
shape = value;
propertySupport.firePropertyChange(PROP_SHAPE, oldValue, shape);
}
propertySupport.addPropertyChangeListener(listener);
}
propertySupport.removePropertyChangeListener(listener);
}
int w = getWidth();
int h = getHeight();
if (shape == "Round Retangle") {
g.drawRoundRect(0,0,w-1,h-1,30,30);
} else if(shape == "Oval") {
g.drawOval(0, 0, w-1, h-1);
} else if (shape == "Arc"){
g.drawArc(0,0,w-1,h-1,0,180);
} else {
g.drawRect(0, 0, w-1, h-1);
}
}
}
import java.beans.PropertyEditorSupport;
*
* @author Administrator
*/
//帮助构建属性编辑器的支持类
public class ShapePropertyEditor extends PropertyEditorSupport{
//getTags()返回属性的全部可选值
public String[] getTags(){
String shapes[] = {
"Rectangle","Round Rectangle","oval","Arc"
};
return shapes;
}
/*在产生代码及设置属性值时被使用,返回当前属性值的java代码,
引号是让属性值作为字符串处理
*/
public String getJavaInitializationString(){
return "/""+getAsText()+"/"";
}
//返回当前属性值的字符串形式
public String getAsText(){
return (String) super.getValue();
}
//用于以字符串的形式设置属性值 这里string是一个字符串型,不用转换
public void setAsText(String string) throws IllegalArgumentException{
super.setValue(string);
}
}
2.3 将属性和属性编辑器关联
import java.beans.BeanDescriptor;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
import myshape.*;
*
* @author Administrator
*/
public class MyShapeBeanInfo extends SimpleBeanInfo {
private final static Class beanClass = MyShape.class;
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor bd = new BeanDescriptor(beanClass);
bd.setDisplayName("MyShapeBean");
return bd;
}
public PropertyDescriptor[] getPropertyDescriptors(){
try{
PropertyDescriptor textPD = new PropertyDescriptor("shape",beanClass);
//为属性shape指定了属性编辑器ShapePropertyEdiotr,
//并生成一个property descriptor数组
textPD.setPropertyEditorClass(ShapePropertyEditor.class);
PropertyDescriptor rv[] = { textPD };
return rv;
} catch(IntrospectionException e) {
throw new Error(e.toString());
}
}
}