




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、圖形用戶界面設(shè)計(jì)實(shí)驗(yàn)1NestedPanelsTheprogramNestedPanels.javaisfromListing3.8ofthetext.Savetheprogramtoyourdirectoryanddothefollowing:1.Compileandruntheprogram.Experimentwithresizingtheframeandobservetheeffectonthecomponents.運(yùn)行程序后出現(xiàn)如下界面:Ig?|NestedPanels改變窗口的大小,觀察到:(見下圖)(1) 兩個(gè)子面板one和two的尺寸都保持不變。(2) 藍(lán)色的主面板隨著窗口的變
2、大而擴(kuò)展。(3) 藍(lán)色面板變長(zhǎng),one和tw。子面板都不變,當(dāng)藍(lán)色面板變寬時(shí),兩個(gè)子面板隨著它移動(dòng),并保持居中狀態(tài)。(4) 縮小窗口,根據(jù)流式布局的形式,two子面板因?yàn)槲恢萌莶幌拢讋?dòng)被放在下一行的位置。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);這一步是運(yùn)行時(shí)讓主面板的大小固定為寬度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);因?yàn)樗{(lán)色主面板的寬320,由于流式布局,一個(gè)一個(gè)把面板加到主面板,MyPanel已經(jīng)占據(jù)了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");/設(shè)置窗口的名稱實(shí)驗(yàn)2VotingwithButtonsFilesVoteCounter.javaandVoteCounterPanel.javacontainslightlymodifiedversionsofPushCounter.javaandPushCounterPanel.javainlistings4
12、.10and4.11ofthetext.Asinthetexttheprogramcountsthenumberoftimesthebuttonispushed;however,itassumes(pretends”)eachpushisavoteforJoesothebuttonandvariableshavebeenrenamedappropriately.1. Compiletheprogram,thenrunittoseehowitworks.每次點(diǎn)擊按鈕一次,會(huì)顯示Joe的得票數(shù):I務(wù)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.點(diǎn)擊按鈕后:4、以重用的思想實(shí)現(xiàn)該界面的代碼如下: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");/設(shè)置窗口的名稱實(shí)驗(yàn)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);運(yùn)行時(shí):色BMIYourheightininches:Yourweightinpounds:Calculate輸入數(shù)字后: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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 酒店加盟合作協(xié)議合同
- 房地產(chǎn)經(jīng)紀(jì)服務(wù)合同書
- 13《花鐘》教學(xué)設(shè)計(jì)-2024-2025學(xué)年語文三年級(jí)下冊(cè)統(tǒng)編版
- 辦公家具定制合同協(xié)議書
- 房屋租賃合同延期協(xié)議
- 新房購(gòu)買合同范本詳解
- 5《草船借箭》(教學(xué)設(shè)計(jì))-2023-2024學(xué)年統(tǒng)編版語文五年級(jí)下冊(cè)
- 4 升華和凝華 教學(xué)設(shè)計(jì)-2024-2025學(xué)年教科版物理八年級(jí)上冊(cè)
- 企業(yè)高層管理人員勞動(dòng)合同
- 1《場(chǎng)景歌》教學(xué)設(shè)計(jì)-2024-2025學(xué)年二年級(jí)上冊(cè)語文統(tǒng)編版
- 微信視頻號(hào)運(yùn)營(yíng)技巧攻略詳解全套
- 2023CSCO非小細(xì)胞肺癌診療指南解讀
- 利息理論期末考試模擬測(cè)試試題含參考答案
- 干部選拔任用程序
- 部編人教版五年級(jí)下冊(cè)道德與法治簡(jiǎn)答題歸納總結(jié)
- 2023高二開學(xué)第一課《蛻變》-主題班會(huì)
- 口服降糖藥物分類詳解課件
- 二級(jí)生物安全實(shí)驗(yàn)室設(shè)計(jì)建造與運(yùn)行管理指南
- 圍手術(shù)期疼痛護(hù)理課件
- 外國(guó)新聞傳播史-張昆課件
- 圓圈正義:作為自由前提的信念
評(píng)論
0/150
提交評(píng)論