版權(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)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年外貿(mào)實(shí)習(xí)業(yè)務(wù)員工作計(jì)劃樣本(三篇)
- 2024年土地租賃建房合同范本(三篇)
- 2024年幼兒園大班配班老師的個(gè)人工作計(jì)劃范例(三篇)
- 2024年小學(xué)衛(wèi)生室工作計(jì)劃模版(二篇)
- 2024年外貿(mào)業(yè)務(wù)員工作計(jì)劃(四篇)
- 2024年國(guó)培個(gè)人研修計(jì)劃例文(二篇)
- 2024年幼兒園小班家長(zhǎng)會(huì)計(jì)劃范例(七篇)
- 2024年工會(huì)工作總結(jié)例文(二篇)
- 2024年危重患者護(hù)理管理制度范本(五篇)
- 2024年地下車位租賃合同標(biāo)準(zhǔn)模板(二篇)
- 自考《學(xué)前教育研究方法》全套課件(完整版)
- 無痛宮腔鏡檢查日間手術(shù)臨床路徑及表單
- 企業(yè)、事業(yè)專職消防隊(duì)訓(xùn)練內(nèi)容及操作規(guī)程
- 中考課外古詩(shī)詞鑒賞
- 語(yǔ)言行為法簡(jiǎn)介(課堂PPT)
- 完形填空20篇(附答案解析)
- GB_T5235-2021 加工鎳及鎳合金牌號(hào)和化學(xué)成分(高清最新版)
- 【課件】高一上學(xué)期英語(yǔ)人教版(2019)必修第一冊(cè)Unit 2 Grammar 現(xiàn)在進(jìn)行時(shí)-
- 甲骨文軟件操作手冊(cè)4操作手冊(cè)
- 寧波大學(xué)機(jī)械原理考研歷年真題(共23頁(yè))
- 新版報(bào)關(guān)單格式
評(píng)論
0/150
提交評(píng)論