您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > JavaBean实例
天博培训机构的编程实例:设计一个包含有一个Label和三个Button的PanelBeans(1)第一步:创建出BeanBean的程序代码importjava.awt.*;importjava.awt.event.*;importjava.util.*;/***设计一个包含有一个Label和三个Button的PanelBeans*/publicclassYesNoPanelextendsPanel{//bean的属性protectedStringmessageText;//ThemessagetodisplayprotectedStringyesLabel;//Textfortheyes,no,&cancelbuttonsprotectedStringnoLabel;protectedStringcancelLabel;//Beans的内部组件protectedButtonyes,no,cancel;protectedLabelmessage;/**Theno-argumentbeanconstructor,withdefaultpropertyvalues*/publicYesNoPanel(){this(YourMessageHere);}publicYesNoPanel(StringmessageText){this(messageText,Yes,No,Cancel);}publicYesNoPanel(StringmessageText,StringyesLabel,StringnoLabel,StringcancelLabel){super();//通过调用基类的构造函数,以获得Panel的一般特性this.messageText=messageText;//Themessagetodisplaythis.yesLabel=yesLabel;//Textfortheyes,no,&cancelbuttonsthis.noLabel=noLabel;this.cancelLabel=cancelLabel;天博培训机构(newBorderLayout(15,15));//Putthemessagelabelinthemiddleofthewindow.message=newLabel(messageText);add(message,BorderLayout.CENTER);//CreateapanelforthePanelbuttonsandputitatthebottom//oftheBeansPanel.SpecifyaFlowLayoutlayoutmanagerforit.PanelbuttonPanel=newPanel();buttonPanel.setLayout(newFlowLayout(FlowLayout.CENTER,25,15));this.add(buttonPanel,BorderLayout.SOUTH);//Createeachspecifiedbutton,specifyingtheactionlistener//andactioncommandforeach,andaddingthemtothebuttonboxyes=newButton();//Createbuttonsno=newButton();cancel=newButton();this.setYesLabel(yesLabel);this.setNoLabel(noLabel);this.setCancelLabel(cancelLabel);//AddthebuttonstothebuttonboxbuttonPanel.add(yes);buttonPanel.add(no);buttonPanel.add(cancel);}//Methodstoqueryallofthebeanproperties.publicStringgetMessageText(){returnmessageText;}publicStringgetYesLabel(){returnyesLabel;}publicStringgetNoLabel(){returnnoLabel;}publicStringgetCancelLabel(){returncancelLabel;天博培训机构}//Methodstosetallofthebeanproperties.publicvoidsetMessageText(StringmessageText){this.messageText=messageText;message.setText(messageText);validate();}publicvoidsetYesLabel(StringnewYesLabel){yesLabel=newYesLabel;yes.setLabel(newYesLabel);yes.setVisible((newYesLabel!=null)&&(newYesLabel.length()0));validate();}publicvoidsetNoLabel(StringnewNoLabel){noLabel=newNoLabel;no.setLabel(newNoLabel);no.setVisible((newNoLabel!=null)&&(newNoLabel.length()0));validate();}publicvoidsetCancelLabel(StringnewCancelLabel){cancelLabel=newCancelLabel;cancel.setLabel(newCancelLabel);cancel.setVisible((newCancelLabel!=null)&&(newCancelLabel.length()0));validate();}publicvoidsetFont(Fontf){super.setFont(f);//Invokethesuperclassmethodmessage.setFont(f);yes.setFont(f);no.setFont(f);cancel.setFont(f);validate();}}Bean的测试容器程序代码importjava.awt.*;importjava.awt.event.*;天博培训机构*;importjava.awt.event.WindowListener;importjava.awt.event.WindowEvent;publicclassBeanTestFrameextendsFrameimplementsWindowListener{YesNoPanelpanelBean;publicBeanTestFrame(){super(JavaBean的测试容器程序);panelBean=newYesNoPanel(Doyoureallywanttoquit?);panelBean.setFont(newFont(Dialog,Font.BOLD,20));panelBean.setBackground(Color.red);this.add(panelBean,BorderLayout.CENTER);this.setSize(400,400);this.addWindowListener(this);this.setVisible(true);}publicstaticvoidmain(String[]args){BeanTestFrameframe=newBeanTestFrame();}publicvoidwindowOpened(WindowEventparm1){//TODO:Addyourcodehere}publicvoidwindowClosing(WindowEventparm1){this.dispose();System.exit(0);}publicvoidwindowClosed(WindowEventparm1){//TODO:Addyourcodehere}publicvoidwindowIconified(WindowEventparm1){//TODO:Addyourcodehere}publicvoidwindowDeiconified(WindowEventparm1){//TODO:Addyourcodehere天博培训机构}publicvoidwindowActivated(WindowEventparm1){//TODO:Addyourcodehere}publicvoidwindowDeactivated(WindowEventparm1){//TODO:Addyourcodehere}}在应用程序中进行测试在BeanBox中进行测试天博培训机构(2)第二步:为Bean增加BeanInfo类(为Bean提供显式的信息如Bean在应用程序构造器中的属性、图标、事件的名称等)importjava.beans.*;importjava.awt.*;/***ThisBeanInfoclassprovidesadditionalinformationabouttheYesNoPanel*beaninadditiontowhatcanbeobtainedthroughintrospectionalone.**/publicclassYesNoPanelBeanInfoextendsSimpleBeanInfo{publicImagegetIcon(inticonKind){if(iconKind==BeanInfo.ICON_COLOR_16x16){Imageimg=this.loadImage(BeanIconColor16.gif);returnimg;天博培训机构}if(iconKind==BeanInfo.ICON_COLOR_32x32){Imageimg=this.loadImage(BeanIconColor32.gif);returnimg;}returnnull;}/*以下代码位JavaeBean提供在应用程序的构造工具中的显示名称字串*/publicBeanDescriptorgetBeanDescriptor(){BeanDescriptorbd=newBeanDescriptor(YesNoPanel.class);//采用缺省的定制文件创建Bean的描述信息bd.setDisplayName(YesNoPanelBean);//在应用程序的构造工具中的显示名称字串returnbd;}/*以下代码显式地声明属性*/publicPropertyDescriptor[]getPropertyDescriptors(){try{PropertyDescriptorforeground=newPropertyDescriptor(foreground,YesNoPanel.class),background=newPropertyDescriptor(background,YesNoPanel.class),font=newPropertyDescriptor(font,YesNoPanel.class),name=newPropertyDescriptor(name,YesNoPanel.class);/*以上为继承来的属性*//*以下为程序中所新增加的属性*/PropertyDescriptormes
本文标题:JavaBean实例
链接地址:https://www.777doc.com/doc-5892535 .html