面向对象程序设计实验报告java实验报告图形用户界面_第1页
面向对象程序设计实验报告java实验报告图形用户界面_第2页
面向对象程序设计实验报告java实验报告图形用户界面_第3页
面向对象程序设计实验报告java实验报告图形用户界面_第4页
面向对象程序设计实验报告java实验报告图形用户界面_第5页
免费预览已结束,剩余19页可下载查看

下载本文档

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

文档简介

1、图形用户界面设计实验1NestedPanelsTheprogramNestedPanels.javaisfromListing3.8ofthetext.Savetheprogramtoyourdirectoryanddothefollowing:1.Compileandruntheprogram.Experimentwithresizingtheframeandobservetheeffectonthecomponents.运行程序后出现如下界面:Ig?|NestedPanels改变窗口的大小,观察到:(见下图)(1) 两个子面板one和two的尺寸都保持不变。(2) 蓝色的主面板随着窗口的变

2、大而扩展。(3) 蓝色面板变长,one和tw。子面板都不变,当蓝色面板变宽时,两个子面板随着它移动,并保持居中状态。(4) 缩小窗口,根据流式布局的形式,two子面板因为位置容不下,白动被放在下一行的位置。2.Modifytheprogrambyaddingathirdsubpanelthatistwiceaswide,butthesameheight,astheothertwosubpanels.Chooseyourownlabelandcolorforthesubpanel(thecolorshouldnotbered,green,orblue).Addthepaneltotheprima

3、rypanelaftertheothertwopanels.修改的代码如下:JPanelsubPanel3=newJPanel();subPanel3.setPreferredSize(newDimension(300,100);ubPanel3.setBackground(Color.red);JLabellabel3=newJLabel("Three");subPanel3.add(label3);primary.add(subPanel3);3.Compileandrunthemodifiedprogram.Again,experimentwithresizingth

4、eframeandobservetheeffectonthecomponents.4.Nowaddastatementtotheprogramtosetthepreferredsizeoftheprimarypanelto320by260.(Whatwouldbethepurposeofthis?).Compileandruntheprogramtoseeifanythingchanged.代码修改:primary.setPreferredSize(newDimension(320,260);这一步是运行时让主面板的大小固定为宽度320,高度260。5.Nowaddanotherpanelwi

5、thbackgroundcolorblueandsize320by20.Adda"MyPanels"labeltothispanelandthenaddthispaneltotheprimarypanelbeforeaddingtheotherpanels.Compileandruntheprogram.Whatwastheeffectofthispanel?代码如下:JPanelsubPanel4=newJPanel();subPanel4.setPreferredSize(newDimension(320,20);subPanel4.setBackground(Colo

6、r.blue);JLabellabel4=newJLabel("MyPanel");subPanel4.add(label4);primary.add(subPanel4);primary.add(subPanel1);primary.add(subPanel2);primary.add(subPanel3);因为蓝色主面板的宽320,由于流式布局,一个一个把面板加到主面板,MyPanel已经占据了320,所以one面板只能去到下一行。同理得Two,Three的布局形式。6、用可重用的思想编写该界面:packagelab3;importjava.awt.Color;impo

7、rtjava.awt.Dimension;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;publicclassNestedPanels1extendsJFrame/Presentstwocoloredpanelsnestedwithinathird./JPanelsubPanel1,subPanel2,subPanel3,subPanel4,primary;JLabellabel1,label2,label3,label4;publicNestedPanels1()label1=newJLa

8、bel("One");label2=newJLabel("Two");label3=newJLabel("Three");label4=newJLabel("MyPanel");subPanel1=newJPanel();subPanel2=newJPanel();subPanel3=newJPanel();subPanel4=newJPanel();primary=newJPanel();subPanel1.setPreferredSize(newDimension(150,100);subPanel2.setP

9、referredSize(newDimension(150,100);subPanel3.setPreferredSize(newDimension(300,100);subPanel4.setPreferredSize(newDimension(320,20);primary.setPreferredSize(newDimension(320,260);subPanel1.setBackground(Color.green);subPanel2.setBackground(Color.red);subPanel3.setBackground(Color.red);subPanel4.setB

10、ackground(Color.blue);primary.setBackground(Color.blue);subPanell.add(labell);subPanel2.add(label2);subPanel3.add(label3);subPanel4.add(label4);primary.add(subPanel4);primary.add(subPanell);primary.add(subPanel2);primary.add(subPanel3);getContentPane().add(primary);pack();setVisible(true);setDefault

11、CloseOperation(JFrame.EXIT_ON_CLOSE);publicstaticvoidmain(String口args)NestedPanels1NP=newNestedPanels1();NP.setTitle("NestedPanels");/设置窗口的名称实验2VotingwithButtonsFilesVoteCounter.javaandVoteCounterPanel.javacontainslightlymodifiedversionsofPushCounter.javaandPushCounterPanel.javainlistings4

12、.10and4.11ofthetext.Asinthetexttheprogramcountsthenumberoftimesthebuttonispushed;however,itassumes(pretends”)eachpushisavoteforJoesothebuttonandvariableshavebeenrenamedappropriately.1. Compiletheprogram,thenrunittoseehowitworks.每次点击按钮一次,会显示Joe的得票数:I务VoteCounterVoteforJoeVotesforJoe:42. Modifytheprog

13、ramsothattherearetwocandidatestovoteforJoeandSam.Todothisyouneedtodothefollowing:a. AddvariablesforSanavotecounter,abutton,andalabel.b. AddanewinnerclassnamedSamButtonListenertolistenforclicksonthebuttonforSam.InstantiateaninstanceoftheclasswhenaddingtheActionListenertothebuttonforSam.c. Addthebutto

14、nandlabelforSamtothepanel.代码如下:*/VoteCounterPanel.java/Demonstratesagraphicaluserinterfaceandeventlistenersto/tallyvotesfortwocandidates,JoeandSam./*packagelab4;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassVoteCounterPanelextendsJPanel(privateintvotesForJoe,votesForSam;priv

15、ateJButtonjoe,Sam;privateJLabellabelJoe,labelSam;publicVoteCounterPanel()(votesForJoe=0;votesForSam=0;joe=newJButton("VoteforJoe");Sam=newJButton("VoteforSam");joe.addActionListener(newJoeButtonListener();Sam.addActionListener(newSamButtonListener();labelJoe=newJLabel("Votes

16、forJoe:"+votesForJoe);labelSam=newJLabel("VotesforSam:"+votesForSam);add(joe);add(labelJoe);add(Sam);add(labelSam);setPreferredSize(newDimension(300,80);setBackground(Color.cyan);privateclassJoeButtonListenerimplementsActionListenerpublicvoidactionPerformed(ActionEventevent)votesForJo

17、e+;labelJoe.setText("VotesforJoe:"+votesForJoe);privateclassSamButtonListenerimplementsActionListenerpublicvoidactionPerformed(ActionEventevent)votesForSam+;labelSam.setText("VotesforSam:"+votesForSam);3、Compileandruntheprogram.点击按钮后:4、以重用的思想实现该界面的代码如下:packagelab4;importjava.awt.

18、Color;importjava.awt.Dimension;importjava.awt.event.*;importjavax.swing.*;implementspublicclassVoteCounter1extendsJFrameActionListenerprivateJPanelVoteCounterPanel;privateintvotesForJoe,votesForSam;privateJButtonjoe,Sam;privateJLabellabelJoe,labelSam;publicVoteCounter1()votesForJoe=0;votesForSam=0;V

19、oteCounterPanel=newJPanel();joe=newJButton("VoteforJoe");Sam=newJButton("VoteforSam");joe.addActionListener(this);Sam.addActionListener(this);labelJoe=newJLabel("VotesforJoe:"+votesForJoe);labelSam=newJLabel("VotesforSam:"+votesForSam);VoteCounterPanel.add(joe

20、);VoteCounterPanel.add(labelJoe);VoteCounterPanel.add(Sam);VoteCounterPanel.add(labelSam);VoteCounterPanel.setPreferredSize(newDimension(300,80);VoteCounterPanel.setBackground(Color.cyan);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);getContentPane().add(VoteCounterPanel);pack();setVisible(true);pu

21、blicvoidactionPerformed(ActionEventevent)/确定事件源if(event.getSource()=joe)(votesForJoe+;labelJoe.setText("VotesforJoe:"+votesForJoe);if(event.getSource()=Sam)votesForSam+;labelSam.setText("VotesforSam:"+votesForSam);publicstaticvoidmain(Stringargs)VoteCounter1VC1=newVoteCounter1();

22、VC1.setTitle("VoteCounter");/设置窗口的名称实验3CalculatingBodyMassIndexBodyMassIndex(BMI)ismeasureofweightthattakesheightintoaccount.Generally,aBMIabove25isconsideredhigh,thatis,likelytoindicatethatanindividualisoverweight.BMIiscalculatedasfollowsforbothmenandwomen:(703*heightininches)/(weightinpo

23、unds)2FilesBMI.javaandBMIPanel.javacontainskeletonsforaprogramthatusesaGUItolettheusercomputetheirBMI.ThisissimilartotheFahrenheitprograminlistings4.12and4.13ofthetext.Fillinthecodeasindicatedbythecommentsandcompileandrunthisprogram;youshouldseetheBMIcalculatordisplayed.填写的代码如下:packagelab4;*/BMIPane

24、l.java/ComputesbodymassindexinaGUI./*importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassBMIPanelextendsJPanel(privateintWIDTH=280;privateintHEIGHT=120;privateJLabelheightLabel,weightLabel,BMILabel,resultLabel;privateJTextFieldheight,weight;privateJButtoncalculate;/SetsuptheGUI./p

25、ublicBMIPanel()(/createlabelsfortheheightandweighttextfieldsheightLabel=newJLabel("Yourheightininches:");weightLabel=newJLabel("Yourweightinpounds:");/createa"thisisyourBMI"labelBMILabel=newJLabel("thisisyourBMI");/createaresultlabeltoholdtheBMIvalueresultLabe

26、l=newJLabel();/createaJTextFieldtoholdtheperson'sheightininches/createaJTextFieldtoholdtheperson'sweightinpoundshweight=newJTextField(5);/createabuttontopresstocalculateBMIca/createaBMIListenerandmakeitlistenforthebuttontobepressedBMIlistenerBl=newBMIIistener();calculate.addActionListener(BL

27、);/addtheheightlabelandheighttextfieldtothepaneladd(heightLabel);add(height);/addtheweightlabelandweighttextfieldtothepaneladd(weightLabel);add(weight);/addthebuttontothepaneladd(calculate);/addtheBMIlabeltothepaneladd(calculate);/addthelabelthatholdstheresulttothepaneladd(resultLabel);/setthesizeof

28、thepaneltotheWIDTHandHEIGHTconstantssetPreferredSize(newDimension(WIDTH,HEIGHT);/setthecolorofthepaneltowhateveryouchoosetcyan);*/Representsanactionlistenerforthecalculatebutton.*privateclassBMIListenerimplementsActionListener(/ComputetheBMIwhenthebuttonispressed/publicvoidactionPerformed(ActionEven

29、tevent)(StringheightText,weightText;intheightVal,weightVal;doublebmi;/getthetextfromtheheightandweighttextfieldsheightText=height.getText();weightText=weight.getText();/UseInteger.parseInttoconvertthetexttointegervaluesheightVal=Integer.parseInt(heightText);weightVal=Integer.parseInt(weightText);/Ca

30、lculatethebmi=703*weightinpounds/(heightininches)A2bmi=(703*weightVal)/(heightVal*heightVal);/Putresultinresultlabel.UseDouble.toStringtoconvertdoubletostring.resultLabel.setText("YourBMIis:"+Double.toString(bmi);运行时:色BMIYourheightininches:Yourweightinpounds:Calculate输入数字后:Yourweightinpoun

31、ds:4uCaJculareYourBMIis;13.0用可重用的思想编写该界面:packagelab4;*/BMIPanel.java/ComputesbodymassindexinaGUI.*importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassBMI1extendsJFrameimplementsActionListenerprivateintWIDTH=280;privateintHEIGHT=120;privateJPanelBMIPanel;privateJLabelheightLabel,we

32、ightLabel,BMILabel,resultLabel;privateJTextFieldheight,weight;privateJButtoncalculate;/SetsuptheGUI./publicBMI1()BMIPanel=newJPanel();heightLabel=newJLabel("Yourheightininches:");weightLabel=newJLabel("Yourweightinpounds:");/createa"thisisyourBMI"labelBMILabel=newJLabel

33、("thisisyourBMI");/createaresultlabeltoholdtheBMIvalueresultLabel=newJLabel();/createaJTextFieldtoholdtheperson'sheightininches/createaJTextFieldtoholdtheperson'sweightinpoundsheweight=newJTextField(5);/createabuttontopresstocalculateBMIcacalculate.addActionListener(this);/addtheheightlabelandheighttextfieldtothepanelB_MIPanel.add(heightLabel);B_MIPanel.add(height);/addtheweightlabelandweighttextfieldtothepanelBMI

温馨提示

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

评论

0/150

提交评论