




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、圖形用戶界面設(shè)計實驗1NestedPanelsTheprogramNestedPanels.javaisfromListing3.8ofthetext.Savetheprogramtoyourdirectoryanddothefollowing:1.Compileandruntheprogram.Experimentwithresizingtheframeandobservetheeffectonthecomponents.運行程序后出現(xiàn)如下界面:Ig?|NestedPanels改變窗口的大小,觀察到:(見下圖)(1) 兩個子面板one和two的尺寸都保持不變。(2) 藍(lán)色的主面板隨著窗口的變
2、大而擴展。(3) 藍(lán)色面板變長,one和tw。子面板都不變,當(dāng)藍(lán)色面板變寬時,兩個子面板隨著它移動,并保持居中狀態(tài)。(4) 縮小窗口,根據(jù)流式布局的形式,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);因為藍(lán)色主面板的寬320,由于流式布局,一個一個把面板加到主面板,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è)置窗口的名稱實驗2VotingwithButtonsFilesVoteCounter.javaandVoteCounterPanel.javacontainslightlymodifiedversionsofPushCounter.javaandPushCounterPanel.javainlistings4
12、.10and4.11ofthetext.Asinthetexttheprogramcountsthenumberoftimesthebuttonispushed;however,itassumes(pretends”)eachpushisavoteforJoesothebuttonandvariableshavebeenrenamedappropriately.1. Compiletheprogram,thenrunittoseehowitworks.每次點擊按鈕一次,會顯示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.點擊按鈕后:4、以重用的思想實現(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è)置窗口的名稱實驗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輸入數(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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 遙感數(shù)據(jù)深度學(xué)習(xí)-洞察闡釋
- 小學(xué)五年級下學(xué)期 Recycle
- 可持續(xù)發(fā)展視角下的風(fēng)險管理-碳中和目標(biāo)下的商品定價-洞察闡釋
- 農(nóng)業(yè)科技產(chǎn)業(yè)園品牌建設(shè)與市場推廣策略
- 深圳初中考試題庫及答案
- 2025合作伙伴協(xié)議書(合同版本)
- 2025機械設(shè)備銷售版合同2
- 2025雙方交易承包保障合同模板
- 智研咨詢發(fā)布-中國航空裝備行業(yè)產(chǎn)業(yè)鏈全景分析及發(fā)展趨勢預(yù)測報告
- 智研咨詢發(fā)布:2024年中國科考船行業(yè)市場現(xiàn)狀及投資前景分析報告
- 人教版數(shù)學(xué)六年級下冊期末測試卷及答案【真題匯編】
- 2024年國家中醫(yī)藥管理局監(jiān)測統(tǒng)計中心招聘7人歷年重點基礎(chǔ)提升難、易點模擬試題(共500題)附帶答案詳解
- DB37∕T242-2021建筑消防設(shè)施檢測技術(shù)規(guī)程
- 2024年燕舞集團限公司公開招聘高頻考題難、易錯點模擬試題(共500題)附帶答案詳解
- 2024年中考道德與法治時事政治試題庫附答案(綜合題)
- 江蘇省蘇州市2023-2024學(xué)年五年級下學(xué)期期中綜合測試數(shù)學(xué)試卷(蘇教版)
- 游戲陪玩-模板參考
- 論藥品管理在藥品安全中的重要性
- 懷來鼎興投資開發(fā)有限公司審計報告
- 健康心理學(xué)孫宏偉重點
- 感染病的預(yù)防和控制措施概述及實施
評論
0/150
提交評論