文档库 最新最全的文档下载
当前位置:文档库 › JavaBean实例

JavaBean实例

JavaBean的编程实例:设计一个包含有一个Label和三个Button的Panel Beans
(1)第一步:创建出Bean
Bean的程序代码
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
* 设计一个包含有一个Label和三个Button的Panel Beans
*/
public class YesNoPanel extends Panel
{
// bean的属性
protected String messageText; // The message to display
protected String yesLabel; // Text for the yes, no, & cancel buttons
protected String noLabel;
protected String cancelLabel;

// Beans的内部组件
protected Button yes, no, cancel;
protected Label message;

/** The no-argument bean constructor, with default property values */
public YesNoPanel()
{
this("Your Message Here");
}

public YesNoPanel(String messageText)
{
this(messageText, "Yes", "No", "Cancel");
}

public YesNoPanel(String messageText, String yesLabel, String noLabel, String cancelLabel)
{
super(); //通过调用基类的构造函数,以获得Panel的一般特性

this.messageText=messageText; // The message to display
this.yesLabel=yesLabel; // Text for the yes, no, & cancel buttons
this.noLabel=noLabel;
this.cancelLabel=cancelLabel;

this.setLayout(new BorderLayout(15, 15));

// Put the message label in the middle of the window.
message = new Label(messageText);
add(message, BorderLayout.CENTER);

// Create a panel for the Panel buttons and put it at the bottom
// of the Beans Panel. Specify a FlowLayout layout manager for it.

Panel buttonPanel = new Panel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 15));
this.add(buttonPanel, BorderLayout.SOUTH);

// Create each specified button, specifying the action listener
// and action command for each, and adding them to the buttonbox
yes = new Button(); // Create buttons
no = new Button();
cancel = new Button();

this.setYesLabel(yesLabel);
this.setNoLabel(noLabel);
this.setCancelLabel(cancelLabel);

// Add the buttons to the button box
buttonPanel.add(yes);
buttonPanel.add(no);
buttonPanel.add(cancel);
}
// Methods to query all of the bean properties.
public String getMessageText()
{
return messageText;
}
public String getYesLabel()
{
return yesLabel;
}
public String getNoLabel()
{
return noLabel;
}
public String getCancelLabel()
{
return cancelLabel;
}

// Methods to set all of the bean properties.
public void setMessageText(String messageText)
{
this.messageText = messageText;
message.setText(messageText);
validate();
}
public void setYesLabel(String newYesLabel)
{
yesLabel = newYesLabel;
yes.setLabel(newYesLabel);
yes.setVisible((newYesLabel != null) && (

newYesLabel.length() > 0));
validate();
}
public void setNoLabel(String newNoLabel)
{
noLabel = newNoLabel;
no.setLabel(newNoLabel);
no.setVisible((newNoLabel != null) && (newNoLabel.length() > 0));
validate();
}
public void setCancelLabel(String newCancelLabel)
{
cancelLabel = newCancelLabel;
cancel.setLabel(newCancelLabel);
cancel.setVisible((newCancelLabel != null) && (newCancelLabel.length() > 0));
validate();
}
public void setFont(Font f)
{
super.setFont(f); // Invoke the superclass method
message.setFont(f);
yes.setFont(f);
no.setFont(f);
cancel.setFont(f);
validate();
}
}
Bean的测试容器程序代码
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
public class BeanTestFrame extends Frame implements WindowListener
{
YesNoPanel panelBean;
public BeanTestFrame()
{
super("Java Bean 的测试容器程序");

panelBean= new YesNoPanel("Do you really want to quit?");
panelBean.setFont(new Font("Dialog",Font.BOLD,20));
panelBean.setBackground(Color.red);

this.add(panelBean,BorderLayout.CENTER);
this.setSize(400,400);
this.addWindowListener(this);
this.setVisible(true);
}
public static void main(String []args)
{
BeanTestFrame frame=new BeanTestFrame();
}

public void windowOpened(WindowEvent parm1) {
// TODO: Add your code here
}

public void windowClosing(WindowEvent parm1)
{
this.dispose();
System.exit(0);
}

public void windowClosed(WindowEvent parm1) {
// TODO: Add your code here
}

public void windowIconified(WindowEvent parm1) {
// TODO: Add your code here
}

public void windowDeiconified(WindowEvent parm1) {
// TODO: Add your code here
}

public void windowActivated(WindowEvent parm1) {
// TODO: Add your code here
}

public void windowDeactivated(WindowEvent parm1) {
// TODO: Add your code here
}
}
在应用程序中进行测试

在BeanBox中进行测试

(2)第二步:为Bean增加BeanInfo类(为Bean提供显式的信息如Bean在应用程序构造器中的属性、图标、事件的名称等)

import java.beans.*;
import java.awt.*;
/**
* This BeanInfo class provides additional information about the YesNoPanel
* bean in addition to what can be obtained through introspection alone.
**/
public class YesNoPanelBeanInfo extends SimpleBeanInfo
{
public Image getIcon(int iconKind)
{ if(iconKind==BeanInfo.ICON_COLOR_16x16)
{
Image img=this.loadImage("BeanIconColor16.gif");
return img;
}
if(iconKind==BeanInfo.ICON_COLOR_32x32)
{
Image img=this.loadImage("BeanIconColor32.gif");
return img;
}
return null;
}

/*以下代码位JavaeBean提供在应用程序的构造工具中的显示名称字串*/

publ

ic BeanDescriptor getBeanDescriptor()
{
BeanDescriptor bd=new BeanDescriptor(YesNoPanel.class); //采用缺省的定制文件创建Bean的描述信息
bd.setDisplayName("YesNoPanelBean"); //在应用程序的构造工具中的显示名称字串
return bd;
}

/*以下代码显式地声明属性*/
public PropertyDescriptor[] getPropertyDescriptors()
{ try
{
PropertyDescriptor foreground=new PropertyDescriptor("foreground",YesNoPanel.class),
background=new PropertyDescriptor("background",YesNoPanel.class),
font=new PropertyDescriptor("font",YesNoPanel.class),
name=new PropertyDescriptor("name",YesNoPanel.class);
/*以上为继承来的属性*
/
/*以下为程序中所新增加的属性*/
PropertyDescriptor messageText=new PropertyDescriptor("messageText",YesNoPanel.class),
yesLabel=new PropertyDescriptor("yesLabel",YesNoPanel.class),
noLabel=new PropertyDescriptor("noLabel",YesNoPanel.class),
cancelLabel=new PropertyDescriptor("cancelLabel",YesNoPanel.class);

/*可以根据应用的需要,将某些属性在应用程序的构造工具中隐藏起来*/
PropertyDescriptor[] pd={
foreground, background,font,name,messageText,yesLabel,noLabel,cancelLabel
};
return pd;
}
catch(IntrospectionException e)
{ throw new Error(e.toString());
}
}

public int getDefaultPropertyIndex()
{ return 4; //将"messageText"作为缺省的属性
}
}
(3)第三步:为Bean增加事件处理的功能,使其可响应用户的鼠标点击事件、焦点事件、鼠标进入退出和按下释放等事件
Bean的程序代码
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.event.*;
//由于在YesNoPanel Bean中覆盖有yes, no, cancel三个Button,message Label以及buttonPanel 面板。因此,应该将这些组件的MouseListener, FocusListener, ActionListener事件转发给YesNoPanel Bean。
public class YesNoPanel extends Panel implements MouseListener, FocusListener, ActionListener
{ // bean的属性
protected String messageText; // The message to display
protected String yesLabel; // Text for the yes, no, & cancel buttons
protected String noLabel;
protected String cancelLabel;

// Beans的内部组件
protected Button yes, no, cancel;
protected Label message;

private transient ActionListener actionListener=null;
private transient MouseListener mouseListener=null;
private transient FocusListener focusListener=null;

/** The no-argument bean constructor, with default property values */
public YesNoPanel()
{
this("Your Message Here");
}

public YesNoPanel(String messageText)
{
this(messageText, "Yes", "No", "Cancel");
}

public YesNoPanel(String messageText, String yesLabel, String noLabel, String cancelLabel)

{
super(); //通过调用基类的构造函数,以获得Panel的一般特性

this.messageText=messageText; // The message to display
this.yesLabel=yesLabel; // Text for the yes, no, & cancel buttons
this.noLabel=noLabel;
this.cancelLabel=cancelLabel;

this.enableEvents(AWTEvent.FOCUS_EVENT_MASK| //必须设置此方法,否则Bean不能接受事件
AWTEvent.MOUSE_EVENT_MASK|
AWTEvent.MOUSE_MOTION_EVENT_MASK|AWTEvent.ACTION_EVENT_MASK);

this.setLayout(new BorderLayout(15, 15));
// Put the message label in the middle of the window.

message = new Label(messageText);
message.addMouseListener(this);
message.addFocusListener(this);
add(message, BorderLayout.CENTER);
// Create a panel for the Panel buttons and put it at the bottom
// of the Beans Panel. Specify a FlowLayout layout manager for it.

Panel buttonPanel = new Panel();
buttonPanel.addMouseListener(this);
buttonPanel.addFocusListener(this);
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 15));
this.add(buttonPanel, BorderLayout.SOUTH);

// Create each specified button, specifying the action listener
// and action command for each, and adding them to the buttonbox
yes = new Button(); // Create buttons
yes.addMouseListener(this);
yes.addActionListener(this);
yes.addFocusListener(this);
no = new Button();
no.addMouseListener(this);
no.addActionListener(this);
no.addFocusListener(this);
cancel = new Button();
cancel.addMouseListener(this);
cancel.addFocusListener(this);
cancel.addActionListener(this);

this.setYesLabel(yesLabel);
this.setNoLabel(noLabel);
this.setCancelLabel(cancelLabel);

// Add the buttons to the button box
buttonPanel.add(yes);
buttonPanel.add(no);
buttonPanel.add(cancel);
}
// Methods to query all of the bean properties.
public String getMessageText()
{
return messageText;
}
public String getYesLabel()
{
return yesLabel;
}
public String getNoLabel()
{
return noLabel;
}
public String getCancelLabel()
{
return cancelLabel;
}

// Methods to set all of the bean properties.
public void setMessageText(String messageText)
{
this.messageText = messageText;
message.setText(messageText);
validate();
}
public void setYesLabel(String newYesLabel)
{
yesLabel = newYesLabel;
yes.setLabel(newYesLabel);
yes.setVisible((newYesLabel != null) && (newYesLabel.length() > 0));
validate();
}
public void setNoLabel(String newNoLabel)
{
noLabel = newNoLabel;
no.setLabel(newNoLabel);
no.setVisible((newNoLabel != null) && (newNoLabel.length() > 0));
validate();
}
public void setCancelLabel(String newCancelLabel)
{
cancelLabel = newCancelLabel;
cancel.setLabel(newCancelLabel);

cancel.setVisible((newCancelLabel != null) && (newCancelLabel.length() > 0));
validate();
}
public void setFont(Font f)
{
super.setFont(f); // Invoke the superclass method
message.setFont(f);
yes.setFont(f);
no.setFont(f);
cancel.setFont(f);
validate();
}

//实现维护鼠标点击事件的监听器列表的方法(添加和删除)
public synchronized void addActionListener(ActionListener listener)
{ actionListener=AWTEventMulticaster.add(actionListener,listener);
}
public synchronized void removeActionListener(ActionListener listener)
{ actionListener=AWTEventMulticaster.remove(actionListener,listener);
}
public synchronized void addMouseListener(MouseListener listener)
{ mouseListener=AWTEventMulticaster.add(mouseListener,listener);
}
public synchronized void removeMouseListener(MouseListener listener)
{ mouseListener=AWTEventMulticaster.remove(mouseListener,listener);
}
public synchronized void addFocusListener(FocusListener listener)
{ focusListener=AWTEventMulticaster.add(focusListener,listener);
}
public synchronized void removeFocusListener(FocusListener listener)
{ focusListener=AWTEventMulticaster.remove(focusListener,listener);
}

//向所有注册ActionListener的组件分发ActionEvent事件
protected void processActionEvent(ActionEvent e)
{ if(actionListener !=null)
{ actionListener.actionPerformed(e);
}
}

protected void processFocusEvent(FocusEvent e)
{ switch(e.getID())
{ case FocusEvent.FOCUS_GAINED:
repaint();
if(focusListener !=null)
{ focusListener.focusGained(e);
}
break;
case FocusEvent.FOCUS_LOST:
repaint();
if(focusListener !=null)
{ focusListener.focusLost(e);
}
break;

}
super.processFocusEvent(e);
}
protected void processMouseEvent(MouseEvent e)
{ switch(e.getID())
{ case MouseEvent.MOUSE_PRESSED:
repaint();
if(mouseListener !=null)
{ mouseListener.mousePressed(e);
}
break;
case MouseEvent.MOUSE_RELEASED:
if(mouseListener !=null)
{ mouseListener.mouseReleased(e);
}
break;
case MouseEvent.MOUSE_CLICKED:
if(mouseListener !=null)
{ mouseListener.mouseClicked(e);
}
break;
case MouseEvent.MOUSE_ENTERED:
if(mouseListener !=null)
{ mouseListener.mouseEntered(e);
}
break;
case MouseEvent.MOUSE_EXITED:
if(mouseListener !=null)
{ mouseListener.mouseExited(e);
}
break;
}
super.processMouseEvent(e);
}
//由于在YesNoPanel Bean中覆盖有yes, no, cancel三个Button,message Label以及buttonPanel 面板。因此,应该将这些组件的MouseListener事件转发给YesNoPanel Bean。
public void mouseClicked(MouseEvent parm1)
{
processMouseEvent(parm1);
}

public void mousePressed(MouseEvent parm1)
{
processMouseEvent(parm1);
}

public void mouseReleased(MouseEvent parm1)
{
proces

sMouseEvent(parm1);
}

public void mouseEntered(MouseEvent parm1)
{
processMouseEvent(parm1);
}

public void mouseExited(MouseEvent parm1)
{
processMouseEvent(parm1);
}
//由于在YesNoPanel Bean中覆盖有yes, no, cancel三个Button,message Label以及buttonPanel 面板。因此,应该将这些组件的FocusListener事件转发给YesNoPanel Bean。
public void focusGained(FocusEvent parm1)
{
processFocusEvent(parm1);
}

public void focusLost(FocusEvent parm1)
{
processFocusEvent(parm1);
}
//由于在YesNoPanel Bean中覆盖有yes, no, cancel三个Button,message Label以及buttonPanel 面板。因此,应该将这些组件的ActionListener事件转发给YesNoPanel Bean。
public void actionPerformed(ActionEvent parm1)
{
processActionEvent(parm1);
}
}
Bean的测试容器程序代码
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.awt.event.*
public class BeanTestFrame extends Frame implements WindowListener, ActionListener, FocusListener, MouseListener
{
YesNoPanel panelBean;
public BeanTestFrame()
{
super("Java Bean 的测试容器程序");

panelBean= new YesNoPanel("Do you really want to quit?");
panelBean.setFont(new Font("Dialog",Font.BOLD,20));
panelBean.setBackground(Color.red);
panelBean.addActionListener(this);
panelBean.addMouseListener(this);
panelBean.addFocusListener(this);

this.add(panelBean,BorderLayout.CENTER);
this.setSize(400,400);
this.addWindowListener(this);
this.setVisible(true);
}
public static void main(String []args)
{
BeanTestFrame frame=new BeanTestFrame();
}
public void windowOpened(WindowEvent parm1) {
// TODO: Add your code here
}
public void windowClosing(WindowEvent parm1)
{
this.dispose();
System.exit(0);
}
public void windowClosed(WindowEvent parm1) {
// TODO: Add your code here
}
public void windowIconified(WindowEvent parm1) {
// TODO: Add your code here
}
public void windowDeiconified(WindowEvent parm1) {
// TODO: Add your code here
}
public void windowActivated(WindowEvent parm1) {
// TODO: Add your code here
}
public void windowDeactivated(WindowEvent parm1) {
// TODO: Add your code here
}
public void actionPerformed(ActionEvent parm1)
{
if(parm1.getSource()==panelBean.yes)
{
this.setTitle("您的鼠标点击了PaneBean并且是Yes按钮!");
}
else if(parm1.getSource()==panelBean.no)
{
this.setTitle("您的鼠标点击了PaneBean并且是No按钮!");
}
else if(parm1.getSource()==panelBean.cancel)
{
this.setTitle("您的鼠标点击了PaneBean并且是Cancel按钮!");
}
}
public void focusGained(FocusEvent parm1)
{
System.out.print("PaneBean获得输入焦点!");
}
public void focusLost(FocusEvent parm1)
{
System.out.print("PaneBean失去输

入焦点!");
}

public void mouseClicked(MouseEvent parm1) {
// TODO: Add your code here
}
public void mousePressed(MouseEvent parm1) {
// TODO: Add your code here
}
public void mouseReleased(MouseEvent parm1) {
// TODO: Add your code here
}
public void mouseEntered(MouseEvent parm1)
{
this.setTitle("您的鼠标进入PaneBean所在的区域!");
}
public void mouseExited(MouseEvent parm1)
{
this.setTitle("您的鼠标离开PaneBean所在的区域!");
}
}
在应用程序中进行测试

YesNoPanelBeanInfo程序代码
import java.beans.*;
import java.awt.*;
/**
* This BeanInfo class provides additional information about the YesNoPanel
* bean in addition to what can be obtained through introspection alone.
**/
public class YesNoPanelBeanInfo extends SimpleBeanInfo
{
public Image getIcon(int iconKind)
{ if(iconKind==BeanInfo.ICON_COLOR_16x16)
{
Image img=this.loadImage("BeanIconColor16.gif");
return img;
}
if(iconKind==BeanInfo.ICON_COLOR_32x32)
{
Image img=this.loadImage("BeanIconColor32.gif");
return img;
}
return null;
}

/*以下代码位JavaeBean提供在应用程序的构造工具中的显示名称字串*/

public BeanDescriptor getBeanDescriptor()
{
BeanDescriptor bd=new BeanDescriptor(YesNoPanel.class); //采用缺省的定制文件创建Bean的描述信息
bd.setDisplayName("YesNoPanelBean"); //在应用程序的构造工具中的显示名称字串
return bd;
}

/*以下代码显式地声明属性*/
public PropertyDescriptor[] getPropertyDescriptors()
{ try
{
PropertyDescriptor foreground=new PropertyDescriptor("foreground",YesNoPanel.class),
background=new PropertyDescriptor("background",YesNoPanel.class),
font=new PropertyDescriptor("font",YesNoPanel.class),
name=new PropertyDescriptor("name",YesNoPanel.class);
/*以上为继承来的属性*
/
/*以下为程序中所新增加的属性*/
PropertyDescriptor messageText=new PropertyDescriptor("messageText",YesNoPanel.class),
yesLabel=new PropertyDescriptor("yesLabel",YesNoPanel.class),
noLabel=new PropertyDescriptor("noLabel",YesNoPanel.class),
cancelLabel=new PropertyDescriptor("cancelLabel",YesNoPanel.class);

/*可以根据应用的需要,将某些属性在应用程序的构造工具中隐藏起来*/
PropertyDescriptor[] pd={
foreground, background,font,name,messageText,yesLabel,noLabel,cancelLabel
};
return pd;
}
catch(IntrospectionException e)
{ throw new Error(e.toString());
}
}

public int getDefaultPropertyIndex()
{ return 4; //将"messageText"作为缺省的属性
}

//以下代码通过实现getEventSetDescriptors()方法来为Bean显式地注册事件接口
public EventSetDescriptor[] getEventSetDescriptors()
{
EventSetDescriptor action=null,focusGained=nu

ll,focusLosted=null,
mouseReleased=null,mousePressed=null,mouseEntered=null,
mouseExited=null,mouseClicked=null;
try
{
action=new EventSetDescriptor(YesNoPanel.class,
"actionPerformed",
java.awt.event.ActionListener.class,
"actionPerformed");
action.setDisplayName("YesNoPanel actionPerformed Event");
focusGained=new EventSetDescriptor(YesNoPanel.class,
"focusGained",
java.awt.event.FocusListener.class,
"focusGained");
focusGained.setDisplayName("YesNoPanel Focus Gained Event");
focusLosted=new EventSetDescriptor(YesNoPanel.class,
"focusLost",
java.awt.event.FocusListener.class,
"focusLost");
focusLosted.setDisplayName("YesNoPanel Focus Losted Event");

mousePressed=new EventSetDescriptor(YesNoPanel.class,
"mousePressed",
java.awt.event.MouseListener.class,
"mousePressed");
mousePressed.setDisplayName("YesNoPanel Mouse Pressed Event");
mouseReleased=new EventSetDescriptor(YesNoPanel.class,
"mouseReleased",
java.awt.event.MouseListener.class,
"mouseReleased");
mouseReleased.setDisplayName("YesNoPanel Mouse Released Event");
mouseEntered=new EventSetDescriptor(YesNoPanel.class,
"mouseEntered",
java.awt.event.MouseListener.class,
"mouseEntered");
mouseEntered.setDisplayName("YesNoPanel Mouse Enter Event");
mouseExited=new EventSetDescriptor(YesNoPanel.class,
"mouseExited",
java.awt.event.MouseListener.class,
"mouseExited");
mouseExited.setDisplayName("YesNoPanel Mouse Exit Event");
mouseClicked=new EventSetDescriptor(YesNoPanel.class,
"mouseClicked",
java.awt.event.MouseListener.class,
"mouseClicked");
mouseClicked.setDisplayName("YesNoPanel Mouse Click Event");
}
catch(IntrospectionException e)
{
}
EventSetDescriptor name[]={
action,focusGained,focusLosted,mouseReleased,
mousePressed,mouseEntered,mouseExited,mouseClicked
};
return name;
}
}
在BeanBox中进行测试


(4)第四步:将messageText属性改变为索引属性,并在BDK中为它提供属性编辑面板
Bean的程序代码
import java.awt.*;


import java.awt.event.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* 设计一个包含有一个Label和三个Button的Panel Beans
*/
//由于在YesNoPanel Bean中覆盖有yes, no, cancel三个Button,message Label以及buttonPanel 面板。
//因此,应该将这些组件的MouseListener, FocusListener, ActionListener事件转发给YesNoPanel Bean。
public class YesNoPanel extends Panel implements MouseListener, FocusListener, ActionListener
{
// bean的属性
protected String messageText[]={"Message One",
"Message Two",
"Message Three",
"Message four"
}; // The message to display
protected String yesLabel; // Text for the yes, no, & cancel buttons
protected String noLabel;
protected String cancelLabel;

// Beans的内部组件
protected Button yes, no, cancel;
protected Label message;

private transient ActionListener actionListener=null;
private transient MouseListener mouseListener=null;
private transient FocusListener focusListener=null;

/** The no-argument bean constructor, with default property values */
public YesNoPanel()
{
this("Your Message Here");
}

public YesNoPanel(String messageText)
{
this(messageText, "Yes", "No", "Cancel");
}

public YesNoPanel(String messageText, String yesLabel, String noLabel, String cancelLabel)
{
super(); //通过调用基类的构造函数,以获得Panel的一般特性

this.messageText[0]=messageText; // The message to display
this.yesLabel=yesLabel; // Text for the yes, no, & cancel buttons
this.noLabel=noLabel;
this.cancelLabel=cancelLabel;

this.enableEvents(AWTEvent.FOCUS_EVENT_MASK| //必须设置此方法,否则Bean不能接受事件
AWTEvent.MOUSE_EVENT_MASK|
AWTEvent.MOUSE_MOTION_EVENT_MASK|AWTEvent.ACTION_EVENT_MASK);

this.setLayout(new BorderLayout(15, 15));

// Put the message label in the middle of the window.
message = new Label(messageText);
message.addMouseListener(this);
message.addFocusListener(this);
add(message, BorderLayout.CENTER);

// Create a panel for the Panel buttons and put it at the bottom
// of the Beans Panel. Specify a FlowLayout layout manager for it.

Panel buttonPanel = new Panel();
buttonPanel.addMouseListener(this);
buttonPanel.addFocusListener(this);
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 15));
this.add(buttonPanel, BorderLayout.SOUTH);

// Create each specified button, specifying the action listener
// and action command for each, and adding them to the buttonbox
yes = new Button(); // Create buttons
yes.

addMouseListener(this);
yes.addActionListener(this);
yes.addFocusListener(this);
no = new Button();
no.addMouseListener(this);
no.addActionListener(this);
no.addFocusListener(this);
cancel = new Button();
cancel.addMouseListener(this);
cancel.addFocusListener(this);
cancel.addActionListener(this);

this.setYesLabel(yesLabel);
this.setNoLabel(noLabel);
this.setCancelLabel(cancelLabel);

// Add the buttons to the button box
buttonPanel.add(yes);
buttonPanel.add(no);
buttonPanel.add(cancel);
}
// Methods to query all of the bean properties.

public String getYesLabel()
{
return yesLabel;
}
public void setYesLabel(String newYesLabel)
{
yesLabel = newYesLabel;
yes.setLabel(newYesLabel);
yes.setVisible((newYesLabel != null) && (newYesLabel.length() > 0));
validate();
}
public String getNoLabel()
{
return noLabel;
}
public void setNoLabel(String newNoLabel)
{
noLabel = newNoLabel;
no.setLabel(newNoLabel);
no.setVisible((newNoLabel != null) && (newNoLabel.length() > 0));
validate();
}
public String getCancelLabel()
{
return cancelLabel;
}
public void setCancelLabel(String newCancelLabel)
{
cancelLabel = newCancelLabel;
cancel.setLabel(newCancelLabel);
cancel.setVisible((newCancelLabel != null) && (newCancelLabel.length() > 0));
validate();
}
public String getMessageText(int index)
{
return messageText[index];
}
public void setMessageText(int index,String messageText)
{
this.messageText[index] = messageText;
message.setText(this.messageText[index]);
validate();
}
public void setMessageText(String [] messageText) /* 设置整个数组 */
{
this.messageText=messageText;
}
public String[] getMessageText() /* 取得整个数组值 */
{
return this.messageText;
}
public void setFont(Font f)
{
super.setFont(f); // Invoke the superclass method
message.setFont(f);
yes.setFont(f);
no.setFont(f);
cancel.setFont(f);
validate();
}

//实现维护鼠标点击事件的监听器列表的方法(添加和删除)
public synchronized void addActionListener(ActionListener listener)
{ actionListener=AWTEventMulticaster.add(actionListener,listener);
}
public synchronized void removeActionListener(ActionListener listener)
{ actionListener=AWTEventMulticaster.remove(actionListener,listener);
}
public synchronized void addMouseListener(MouseListener listener)
{ mouseListener=AWTEventMulticaster.add(mouseListener,listener);
}
public synchronized void removeMouseListener(MouseListener listener)
{ mouseListener=AWTEventMulticaster.remove(mouseListener,listener);
}
public synchronized void addFocusListener(FocusListener listener)
{ focusListener=AWTEventMulticaster.add(focusListener,listener);
}
publ

ic synchronized void removeFocusListener(FocusListener listener)
{ focusListener=AWTEventMulticaster.remove(focusListener,listener);
}
//向所有注册ActionListener的组件分发ActionEvent事件
protected void processActionEvent(ActionEvent e)
{ if(actionListener !=null)
{ actionListener.actionPerformed(e);
}
}

protected void processFocusEvent(FocusEvent e)
{ switch(e.getID())
{ case FocusEvent.FOCUS_GAINED:
repaint();
if(focusListener !=null)
{ focusListener.focusGained(e);
}
break;
case FocusEvent.FOCUS_LOST:
repaint();
if(focusListener !=null)
{ focusListener.focusLost(e);
}
break;

}
super.processFocusEvent(e);
}
protected void processMouseEvent(MouseEvent e)
{ switch(e.getID())
{ case MouseEvent.MOUSE_PRESSED:
repaint();
if(mouseListener !=null)
{ mouseListener.mousePressed(e);
}
break;
case MouseEvent.MOUSE_RELEASED:
if(mouseListener !=null)
{ mouseListener.mouseReleased(e);
}
break;
case MouseEvent.MOUSE_CLICKED:
if(mouseListener !=null)
{ mouseListener.mouseClicked(e);
}
break;
case MouseEvent.MOUSE_ENTERED:
if(mouseListener !=null)
{ mouseListener.mouseEntered(e);
}
break;
case MouseEvent.MOUSE_EXITED:
if(mouseListener !=null)
{ mouseListener.mouseExited(e);
}
break;
}
super.processMouseEvent(e);
}

//由于在YesNoPanel Bean中覆盖有yes, no, cancel三个Button,message Label以及buttonPanel 面板。因此,应该将这些组件的MouseListener事件转发给YesNoPanel Bean。
public void mouseClicked(MouseEvent parm1)
{
processMouseEvent(parm1);
}
public void mousePressed(MouseEvent parm1)
{
processMouseEvent(parm1);
}
public void mouseReleased(MouseEvent parm1)
{
processMouseEvent(parm1);
}
public void mouseEntered(MouseEvent parm1)
{
processMouseEvent(parm1);
}
public void mouseExited(MouseEvent parm1)
{
processMouseEvent(parm1);
}
//由于在YesNoPanel Bean中覆盖有yes, no, cancel三个Button,message Label以及buttonPanel 面板。因此,应该将这些组件的FocusListener事件转发给YesNoPanel Bean。
public void focusGained(FocusEvent parm1)
{
processFocusEvent(parm1);
}

public void focusLost(FocusEvent parm1)
{
processFocusEvent(parm1);
}
//由于在YesNoPanel Bean中覆盖有yes, no, cancel三个Button,message Label以及buttonPanel 面板。因此,应该将这些组件的ActionListener事件转发给YesNoPanel Bean。
public void actionPerformed(ActionEvent parm1)
{
processActionEvent(parm1);
}
}
YesNoPanelBeanInfo程序代码
import java.beans.*;
import java.awt.*;
public class YesNoPanelBeanInfo extends SimpleBeanInfo
{
public Image getIcon(int iconKind)
{ if(iconKind==BeanInfo.ICON_COLOR_16x16)
{
Image img=this.loadImage("BeanIconColor16.gif");
return img;

}
if(iconKind==BeanInfo.ICON_COLOR_32x32)
{
Image img=this.loadImage("BeanIconColor32.gif");
return img;
}
return null;
}

/*以下代码位JavaeBean提供在应用程序的构造工具中的显示名称字串*/
public BeanDescriptor getBeanDescriptor()
{
BeanDescriptor bd=new BeanDescriptor(YesNoPanel.class); //采用缺省的定制文件创建Bean的描述信息
bd.setDisplayName("YesNoPanelBean"); //在应用程序的构造工具中的显示名称字串
return bd;
}
/*以下代码显式地声明属性*/
public PropertyDescriptor[] getPropertyDescriptors()
{ try
{ PropertyDescriptor foreground=new PropertyDescriptor("foreground",YesNoPanel.class),
background=new PropertyDescriptor("background",YesNoPanel.class),
font=new PropertyDescriptor("font",YesNoPanel.class),
name=new PropertyDescriptor("name",YesNoPanel.class);
/*以上为继承来的属性*
/
/*以下为程序中所新增加的属性*/
PropertyDescriptor messageText=new PropertyDescriptor("messageText",YesNoPanel.class),
yesLabel=new PropertyDescriptor("yesLabel",YesNoPanel.class),
noLabel=new PropertyDescriptor("noLabel",YesNoPanel.class),
cancelLabel=new PropertyDescriptor("cancelLabel",YesNoPanel.class);

messageText.setPropertyEditorClass(YesNoPanelMessageTextEditor.class); //设置面板所在的类
/*可以根据应用的需要,将某些属性在应用程序的构造工具中隐藏起来*/
PropertyDescriptor[] pd={
foreground, background,font,name,messageText,yesLabel,noLabel,cancelLabel
};
return pd;
}
catch(IntrospectionException e)
{ throw new Error(e.toString());
}
}
public int getDefaultPropertyIndex()
{ return 4; //将"messageText"作为缺省的属性
}

//以下代码通过实现getEventSetDescriptors()方法来为Bean显式地注册事件接口
public EventSetDescriptor[] getEventSetDescriptors()
{
EventSetDescriptor action=null,focusGained=null,focusLosted=null,
mouseReleased=null,mousePressed=null,mouseEntered=null,
mouseExited=null,mouseClicked=null;
try
{
action=new EventSetDescriptor(YesNoPanel.class,
"actionPerformed",
java.awt.event.ActionListener.class,
"actionPerformed");
action.setDisplayName("YesNoPanel actionPerformed Event");
focusGained=new EventSetDescriptor(YesNoPanel.class,
"focusGained",
java.awt.event.FocusListener.class,
"focusGained");
focusGained.setDisplayName("YesNoPanel Focus Gained Event");
focusLosted=new EventSetDescriptor(YesNoPanel.class,
"focusLost",
java.awt.event.FocusListener

.class,
"focusLost");
focusLosted.setDisplayName("YesNoPanel Focus Losted Event");

mousePressed=new EventSetDescriptor(YesNoPanel.class,
"mousePressed",
java.awt.event.MouseListener.class,
"mousePressed");
mousePressed.setDisplayName("YesNoPanel Mouse Pressed Event");
mouseReleased=new EventSetDescriptor(YesNoPanel.class,
"mouseReleased",
java.awt.event.MouseListener.class,
"mouseReleased");
mouseReleased.setDisplayName("YesNoPanel Mouse Released Event");
mouseEntered=new EventSetDescriptor(YesNoPanel.class,
"mouseEntered",
java.awt.event.MouseListener.class,
"mouseEntered");
mouseEntered.setDisplayName("YesNoPanel Mouse Enter Event");
mouseExited=new EventSetDescriptor(YesNoPanel.class,
"mouseExited",
java.awt.event.MouseListener.class,
"mouseExited");
mouseExited.setDisplayName("YesNoPanel Mouse Exit Event");
mouseClicked=new EventSetDescriptor(YesNoPanel.class,
"mouseClicked",
java.awt.event.MouseListener.class,
"mouseClicked");
mouseClicked.setDisplayName("YesNoPanel Mouse Click Event");
}
catch(IntrospectionException e)
{
}
EventSetDescriptor name[]={
action,focusGained,focusLosted,mouseReleased,
mousePressed,mouseEntered,mouseExited,mouseClicked
};
return name;
}
}
YesNoPanel Bean的 MessageText 属性的Editor面板所在的类程序代码
import java.beans.*;
public class YesNoPanelMessageTextEditor extends PropertyEditorSupport
{
public String[] getTags()
{ String values[] = { "Message One",
"Message Two",
"Message Three",
"Message four"
};
return values;
}
}
makejar 批处理命令为(增加YesNoPanelMessageTextEditor.class类文件以打包)
jar cfm YesNoPanel.jar JavaBeanManifest.txt YesNoPanel.class YesNoPanelBeanInfo.class BeanIconColor32.gif BeanIconColor16.gif YesNoPanelMessageTextEditor.class
在BDK中的测试如下

(5)第五步:为MessageText属性以及三个按钮的Label属性创建一个定制的属性编辑器。
YesNoPanelCustomizer类的程序代码
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
public class YesNoPanelCustomizer extends Panel implements Customizer, TextListener
{ protected YesNoPanel targetBean ; // The bean being customized
protected TextArea message; // For entering the message
protected Te

xtField fields[]; // For entering button text
public void setObject(Object bean)
{ targetBean = (YesNoPanel)bean; // save the object we're customizing
// Put a label at the top of the panel.
this.setLayout(new BorderLayout());
this.add(new Label("Enter the message to appear in the panel:"), BorderLayout.NORTH);

// And a big text area below it for entering the message.
message = new TextArea(targetBean .getMessageText(0));
message.addTextListener(this);
// TextAreas don't know how big they want to be. You must tell them.
message.setSize(400, 200);
this.add(message, BorderLayout.CENTER);

// Then add a row of textfields for entering the button labels.
Panel buttonbox = new Panel(); // The row container
buttonbox.setLayout(new GridLayout(1, 0, 25, 10)); // Equally spaced
this.add(buttonbox, BorderLayout.SOUTH); // Put row on bottom

// Now go create three TextFields to put in this row. But actually
// position a Label above each, so create an container for each
// TextField+Label combination.
fields = new TextField[3]; // Array of TextFields.
String[] threeButtonLabels = new String[]
{ // Labels for each.
"Yes Button Label",
"No Button Label",
"Cancel Button Label"
};
String[] threeButtonLabelsValues = new String[]
{ // Initial values of each.
targetBean .getYesLabel(),
targetBean .getNoLabel(),
targetBean .getCancelLabel()
};
for(int i = 0; i < 3; i++)
{
Panel p = new Panel(); // Create a container.
p.setLayout(new BorderLayout()); // Give it a BorderLayout.
p.add(new Label(threeButtonLabels[i]), BorderLayout.NORTH); // Put a label on the top.
fields[i] = new TextField(threeButtonLabelsValues[i]); // Create the text field.
p.add(fields[i], BorderLayout.CENTER); // Put it below the label.
fields[i].addTextListener(this); // Set the event listener.
buttonbox.add(p); // Add container to row.
}
}
// Add some space around the outside of the panel.
public Insets getInsets()
{
return new Insets(10, 10, 10, 10);
}
public void textValueChanged(TextEvent e)
{
TextComponent t = (TextComponent)e.getSource(); //获得输入条的源对象
String s = t.getText();
if (t == message)
{
targetBean .setMessageText(0,s);
}
else if (t == fields[0])
{
targetBean .setYesLabel(s);
}
else if (t == fields[1])
{
targetBean .setNoLabel(s);
}
else if (t == fields[2])
{
targetBean .setCancelLabel(s);
}
}
}

YesNoPanelBeanInfo 类的程序代码
import java.beans.*;
import java.awt.*;
/**
* This BeanInfo class provides additional information about the YesNoPanel
* bean in

addition to what can be obtained through introspection alone.
**/
public class YesNoPanelBeanInfo extends SimpleBeanInfo
{
public Image getIcon(int iconKind)
{ if(iconKind==BeanInfo.ICON_COLOR_16x16)
{
Image img=this.loadImage("BeanIconColor16.gif");
return img;
}
if(iconKind==BeanInfo.ICON_COLOR_32x32)
{
Image img=this.loadImage("BeanIconColor32.gif");
return img;
}
return null;
}
/*以下代码位JavaeBean提供在应用程序的构造工具中的显示名称字串*/
public BeanDescriptor getBeanDescriptor()
{
BeanDescriptor bd=new BeanDescriptor(YesNoPanel.class,YesNoPanelCustomizer.class); //采用缺省的定制文件创建Bean的描述信息
bd.setDisplayName("YesNoPanelBean"); //在应用程序的构造工具中的显示名称字串
return bd;
}
/*以下代码显式地声明属性*/
public PropertyDescriptor[] getPropertyDescriptors()
{ try
{
PropertyDescriptor foreground=new PropertyDescriptor("foreground",YesNoPanel.class),
background=new PropertyDescriptor("background",YesNoPanel.class),
font=new PropertyDescriptor("font",YesNoPanel.class),
name=new PropertyDescriptor("name",YesNoPanel.class);
/*以上为继承来的属性*
/
/*以下为程序中所新增加的属性*/
PropertyDescriptor messageText=new PropertyDescriptor("messageText",YesNoPanel.class),
yesLabel=new PropertyDescriptor("yesLabel",YesNoPanel.class),
noLabel=new PropertyDescriptor("noLabel",YesNoPanel.class),
cancelLabel=new PropertyDescriptor("cancelLabel",YesNoPanel.class);

messageText.setPropertyEditorClass(YesNoPanelMessageTextEditor.class);//设置面板所在的类
/*可以根据应用的需要,将某些属性在应用程序的构造工具中隐藏起来*/
PropertyDescriptor[] pd={
foreground, background,font,name,messageText,yesLabel,noLabel,cancelLabel
};
return pd;
}
catch(IntrospectionException e)
{ throw new Error(e.toString());
}
}

public int getDefaultPropertyIndex()
{ return 4; //将"messageText"作为缺省的属性
}

//以下代码通过实现getEventSetDescriptors()方法来为Bean显式地注册事件接口
public EventSetDescriptor[] getEventSetDescriptors()
{
EventSetDescriptor action=null,focusGained=null,focusLosted=null,
mouseReleased=null,mousePressed=null,mouseEntered=null,
mouseExited=null,mouseClicked=null;
try
{
action=new EventSetDescriptor(YesNoPanel.class,
"actionPerformed",
java.awt.event.ActionListener.class,
"actionPerformed");
action.setDisplayName("YesNoPanel actionPerformed Event");
focusGained=new EventSetDescriptor(YesNoPanel.class,
"focusGained",

java.awt.event.FocusListener.class,
"focusGained");
focusGained.setDisplayName("YesNoPanel Focus Gained Event");
focusLosted=new EventSetDescriptor(YesNoPanel.class,
"focusLost",
java.awt.event.FocusListener.class,
"focusLost");
focusLosted.setDisplayName("YesNoPanel Focus Losted Event");

mousePressed=new EventSetDescriptor(YesNoPanel.class,
"mousePressed",
java.awt.event.MouseListener.class,
"mousePressed");
mousePressed.setDisplayName("YesNoPanel Mouse Pressed Event");
mouseReleased=new EventSetDescriptor(YesNoPanel.class,
"mouseReleased",
java.awt.event.MouseListener.class,
"mouseReleased");
mouseReleased.setDisplayName("YesNoPanel Mouse Released Event");
mouseEntered=new EventSetDescriptor(YesNoPanel.class,
"mouseEntered",
java.awt.event.MouseListener.class,
"mouseEntered");
mouseEntered.setDisplayName("YesNoPanel Mouse Enter Event");
mouseExited=new EventSetDescriptor(YesNoPanel.class,
"mouseExited",
java.awt.event.MouseListener.class,
"mouseExited");
mouseExited.setDisplayName("YesNoPanel Mouse Exit Event");
mouseClicked=new EventSetDescriptor(YesNoPanel.class,
"mouseClicked",
java.awt.event.MouseListener.class,
"mouseClicked");
mouseClicked.setDisplayName("YesNoPanel Mouse Click Event");
}
catch(IntrospectionException e)
{
}
EventSetDescriptor name[]={
action,focusGained,focusLosted,mouseReleased,
mousePressed,mouseEntered,mouseExited,mouseClicked
};
return name;
}
}


makejar 批处理命令为(增加YesNoPanelCustomizer.class类文件以打包)
jar cfm YesNoPanel.jar JavaBeanManifest.txt YesNoPanel.class YesNoPanelBeanInfo.class BeanIconColor32.gif BeanIconColor16.gif YesNoPanelMessageTextEditor.class YesNoPanelCustomizer.class

相关文档