JavaBean实例.doc_第1页
JavaBean实例.doc_第2页
JavaBean实例.doc_第3页
JavaBean实例.doc_第4页
JavaBean实例.doc_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JavaBean的编程实例:设计一个包含有一个Label和三个Button的Panel Beans(1)第一步:创建出BeanBean的程序代码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 buttonboxyes = new Button(); / Create buttonsno = new Button();cancel = new Button();this.setYesLabel(yesLabel);this.setNoLabel(noLabel);this.setCancelLabel(cancelLabel);/ Add the buttons to the button boxbuttonPanel.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 methodmessage.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 WindowListenerYesNoPanel 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 herepublic void windowClosing(WindowEvent parm1) this.dispose();System.exit(0);public void windowClosed(WindowEvent parm1) / TODO: Add your code herepublic void windowIconified(WindowEvent parm1) / TODO: Add your code herepublic void windowDeiconified(WindowEvent parm1) / TODO: Add your code herepublic void windowActivated(WindowEvent parm1) / TODO: Add your code herepublic 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提供在应用程序的构造工具中的显示名称字串*/public BeanDescriptor getBeanDescriptor()BeanDescriptor bd=new BeanDescriptor(YesNoPanel.class); /采用缺省的定制文件创建Bean的描述信息bd.setDisplayName(YesNoPanelBean);/在应用程序的构造工具中的显示名称字串return bd;/*以下代码显式地声明属性*/public PropertyDescriptor getPropertyDescriptors()tryPropertyDescriptor 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 buttonboxyes = new Button(); / Create buttonsyes.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 boxbuttonPanel.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 methodmessage.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;cessFocusEvent(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;cessMouseEvent(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);Bean的测试容器程序代码import java.awt.*;import java.awt.event.*;import java.beans.*;import java.awt.event.*public class BeanTestFrame extends Frame implements WindowListener, ActionListener, FocusListener, MouseListenerYesNoPanel 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 herepublic void windowClosing(WindowEvent parm1) this.dispose();System.exit(0);public void windowClosed(WindowEvent parm1) / TODO: Add your code herepublic void windowIconified(WindowEvent parm1) / TODO: Add your code herepublic void windowDeiconified(WindowEvent parm1) / TODO: Add your code herepublic void windowActivated(WindowEvent parm1) / TODO: Add your code herepublic void windowDeactivated(WindowEvent parm1) / TODO: Add your code herepublic 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 herepublic void mousePressed(MouseEvent parm1) / TODO: Add your code herepublic void mouseReleased(MouseEvent parm1) / TODO: Add your code herepublic 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;retu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论