




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、圖形用戶界面設(shè)計實驗1NestedPanelsTheprogramNestedPanels.javaisfromListing3.8ofthetext.Savetheprogramtoyourdirectoryanddothefollowing:1.Compileandruntheprogram.Experimentwithresizingtheframeandobservetheeffectonthecomponents.運行程序后出現(xiàn)如下界面:Ig?|NestedPanels改變窗口的大小,觀察到:(見下圖)(1) 兩個子面板one和two的尺寸都保持不變。(2) 藍色的主面板隨著窗口的變
2、大而擴展。(3) 藍色面板變長,one和tw。子面板都不變,當(dāng)藍色面板變寬時,兩個子面板隨著它移動,并保持居中狀態(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);因為藍色主面板的寬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)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 游戲公司企業(yè)文化
- Photoshop平面設(shè)計基礎(chǔ) 課件 任務(wù)5.3 制作旅行社T型廣告牌海報
- 住宅租賃安全責(zé)任與裝修安全協(xié)議
- 地質(zhì)災(zāi)害監(jiān)測測量員聘請與預(yù)警協(xié)議
- 車輛安全保險理賠處理協(xié)議
- 通信材料回購方案
- 住宅小區(qū)停車場租賃合同規(guī)范范本及停車管理
- 餐飲企業(yè)股權(quán)轉(zhuǎn)讓及品牌推廣權(quán)合同
- 太倉歷史面試題及答案
- 手繪校園面試題及答案
- 【電商直播對消費者購買行為影響:以抖音直播為例開題報告1800字】
- 抑郁病診斷證明書
- 氣體分析儀檢定規(guī)程
- 2024-2029年吞咽困難飲食增稠劑行業(yè)市場現(xiàn)狀供需分析及市場深度研究發(fā)展前景及規(guī)劃投資研究報告
- (高清版)WST 348-2024 尿液標(biāo)本的采集與處理
- FZT 73012-2017 文胸行業(yè)標(biāo)準(zhǔn)
- 肺系病的中醫(yī)護理
- 四型機場方案
- 體育運動與勞動教學(xué)計劃結(jié)合
- 腫瘤登記基本技術(shù)腫瘤命名與編碼課件
- 全國各省市縣-一覽表
評論
0/150
提交評論