资源描述:
《JavaBean实例》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、JavaBean的编程实例:设计一个包含有一个Label和三个Button的PanelBeans(1)第一步:创建出BeanBean的程序代码importjava.awt.*;importjava.awt.event.*;importjava.util.*;/***设计一个包含有一个Label和三个Button的PanelBeans*/publicclassYesNoPanelextendsPanel{//bean的属性protectedStringmessageText;//ThemessagetodisplayprotectedStringyesLabel
2、;//Textfortheyes,no,&cancelbuttonsprotectedStringnoLabel;protectedStringcancelLabel;//Beans的内部组件protectedButtonyes,no,cancel;protectedLabelmessage;/**Theno-argumentbeanconstructor,withdefaultpropertyvalues*/publicYesNoPanel(){this("YourMessageHere");}publicYesNoPanel(StringmessageTe
3、xt){this(messageText,"Yes","No","Cancel");}publicYesNoPanel(StringmessageText,StringyesLabel,StringnoLabel,StringcancelLabel){super();//通过调用基类的构造函数,以获得Panel的一般特性this.messageText=messageText;//Themessagetodisplaythis.yesLabel=yesLabel;//Textfortheyes,no,&cancelbuttonsthis.noLabel=noL
4、abel;this.cancelLabel=cancelLabel;天博培训机构Http://www.px1987.comthis.setLayout(newBorderLayout(15,15));//Putthemessagelabelinthemiddleofthewindow.message=newLabel(messageText);add(message,BorderLayout.CENTER);//CreateapanelforthePanelbuttonsandputitatthebottom//oftheBeansPanel.Specifya
5、FlowLayoutlayoutmanagerforit.PanelbuttonPanel=newPanel();buttonPanel.setLayout(newFlowLayout(FlowLayout.CENTER,25,15));this.add(buttonPanel,BorderLayout.SOUTH);//Createeachspecifiedbutton,specifyingtheactionlistener//andactioncommandforeach,andaddingthemtothebuttonboxyes=newButton()
6、;//Createbuttonsno=newButton();cancel=newButton();this.setYesLabel(yesLabel);this.setNoLabel(noLabel);this.setCancelLabel(cancelLabel);//AddthebuttonstothebuttonboxbuttonPanel.add(yes);buttonPanel.add(no);buttonPanel.add(cancel);}//Methodstoqueryallofthebeanproperties.publicStringge
7、tMessageText(){returnmessageText;}publicStringgetYesLabel(){returnyesLabel;}publicStringgetNoLabel(){returnnoLabel;}publicStringgetCancelLabel(){returncancelLabel;天博培训机构Http://www.px1987.com}//Methodstosetallofthebeanproperties.publicvoidsetMessageText(StringmessageText){this.messag
8、eText=messageText;m