




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)名稱】實(shí)驗(yàn) 5 Java GUI 實(shí)驗(yàn)?zāi)康摹?、學(xué)習(xí)和掌握 Java 常用的 GUI 組件。2、學(xué)習(xí)和掌握 Java 常用的布局管理器。3、實(shí)習(xí)和掌握 Java 的事件驅(qū)動(dòng)程序設(shè)計(jì)方法。4、能夠?qū)崿F(xiàn)簡單的 Java GUI 。 【實(shí)驗(yàn)步驟(解題過程) 】1、第一步, Java GUI 靜態(tài)界面的設(shè)計(jì)和實(shí)現(xiàn),包括:組件和組件的擺放(布 局管理器)。2、第二步, Java GUI 動(dòng)態(tài)事件處理的設(shè)計(jì)與實(shí)現(xiàn),需要 Java 事件驅(qū)動(dòng)模型。3、第三步,如果需要實(shí)體類支持,則按實(shí)驗(yàn)2 的步驟實(shí)現(xiàn)實(shí)體類并使用?!緦?shí)驗(yàn)內(nèi)容】1、(移動(dòng)小球)編寫一個(gè)程序,在面板上移動(dòng)小球。定義一個(gè)面板類來顯示小球,并提供
2、想做、向右、向上和向下移動(dòng)小球的方法。說明:程序來源編程練習(xí)16.3 (P456 )。可以不考慮小 球移動(dòng)到邊界外的情況?!緦?shí)驗(yàn)預(yù)習(xí)】1. 問題描述:創(chuàng)建一個(gè)界面,通過鼠標(biāo)點(diǎn)擊按鈕實(shí)現(xiàn)小球的上下左右移動(dòng)。2. 輸入;鼠標(biāo)點(diǎn)擊按鈕;處理:通過事件監(jiān)聽,實(shí)現(xiàn)圓的重畫;輸出;顯示出 移動(dòng)效果。3. 技術(shù)支持;需要用到事件監(jiān)聽,及圓的繪制即圖形知識,布局管理器等。 【附編程代碼】import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.*; importjava.a
3、wt.*;import java.awt.event.*;public class MovingBall extends JFrame private static JButton bleft=new JButton("向左 ");private static JButton bright=new JButton("向右 ");private static JButton bup=new JButton("向上 ");private static JButton bdown=new JButton("向下 ");p
4、rivate CirclePanel canvas = new CirclePanel();public MovingBall() JPanel panel = new JPanel(); /將按鈕放入面板中panel.add(bleft);panel.add(bright); panel.add(bup);panel.add(bdown);this.add(canvas, BorderLayout.CENTER); / 將含有圓的面板放在中央 this.add(panel, BorderLayout.SOUTH); / 將含有按鈕的面板放在右邊 / 創(chuàng)建按鈕的監(jiān)聽器并注冊到按鈕bleft.a
5、ddActionListener(new Listener();bright.addActionListener(new Listener();bup.addActionListener(new Listener(); bdown.addActionListener(new Listener();/ 主方法public static void main(String args) JFrame frame = new MovingBall();frame.setTitle(" 移動(dòng)的小球 "); frame.setLocationRelativeTo(null); / Cen
6、ter the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(480, 300);frame.setVisible(true);/ 監(jiān)聽類class Listener implements ActionListener public void actionPerformed(ActionEvent e) System.out.println(new java.util.Date(e.getWhen();if (e.getSource() = bleft)canvas.left();else i
7、f (e.getSource() = bright)canvas.right();else if (e.getSource() =bup)canvas.up();else if (e.getSource() = bdown)canvas.down();/ 含有一個(gè)圓的面板的類class CirclePanel extends JPanel private int x =0;private int y = 0;/ 左移這個(gè)圓public void left() x-;repaint();/ 右移這個(gè)圓public void right() x+; repaint();/ 上 移 這 個(gè) 圓pub
8、lic void up() y-;repai nt();/下移這個(gè)圓public void dow n() y+;repa in t();/ 重畫這個(gè)圓protected void pain tComp onen t(Graphics g) super.pa in tComp onen t(g);g.drawOval(getWidth()/2+x,getHeight()/2+y,10, 10);【實(shí)驗(yàn)過程及運(yùn)行結(jié)果】在實(shí)驗(yàn)中遇到的問題是不會(huì)畫圓,經(jīng)過查書知道可以使用drawOval方法畫圓2、(創(chuàng)建一個(gè)簡單的計(jì)算器)編寫一個(gè)程序完成加、減、乘、除操作。圖5簡單的計(jì)算器說明:程序來源編程練習(xí)16
9、.4 ( P456 )。文本框組件javax. swing.JTextField, 請參見教材17.6小節(jié)(P473),或者Java API文檔。字符串與數(shù)值之間的轉(zhuǎn)換, 請參見2.18.1小節(jié)(P44 )和9.2.10小節(jié)(P247),或者使用字符串格式化方 法 9.2.11( P248 )?!緦?shí)驗(yàn)預(yù)習(xí)】1. 問題描述:創(chuàng)建一個(gè)界面,利用鼠標(biāo)點(diǎn)擊加減乘除按鈕,實(shí)現(xiàn)兩個(gè)數(shù)之間的 運(yùn)算。2. 輸入:在文本域中輸入兩個(gè)數(shù)字,并用鼠標(biāo)點(diǎn)擊按鈕。處理:創(chuàng)建一個(gè)監(jiān)聽類,對按鈕進(jìn)行監(jiān)聽聽,計(jì)算兩個(gè)數(shù)字之間的運(yùn)算。輸出:在結(jié)果文本域中顯示運(yùn)算結(jié)果3. 技術(shù)要求:需要使用布局管理器, 事件監(jiān)聽,及字符串與數(shù)字
10、之間的轉(zhuǎn)換等。 【附編程代碼】import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class easyCalculation extends JFrame private JButton Add=new JButton
11、(" 加 "); private JButton Subtract=new JButton(" 減 "); private JButton Multiply=new JButton(" 乘 "); private JButton Divide=new JButton("除 ");private JTextField one=new JTextField(5);private JTextField second=new JTextField(5);private JTextField all=new JTextFie
12、ld(10); private JLabel a=new JLabel("第一個(gè)數(shù) ");第二個(gè)數(shù) "); 結(jié)果 ");private JLabel b=new JLabel( private JLabel c=new JLabel(public easyCalculation()FlowLayout panel=new FlowLayout(FlowLayout.CENTER, 20, 50); setLayout(panel);add(a); add(one); add(b); add(second); add(c); add(all);add(Ad
13、d); add(Subtract); add(Multiply); add(Divide); Add.addActionListener(new Listener(); Subtract.addActionListener(new Listener(); Multiply.addActionListener(new Listener(); Divide.addActionListener(new Listener();/ 主方法public static void main(String args) easyCalculation frame = new easyCalculation();f
14、rame.setTitle(" 簡單的計(jì)算器 ");frame.setLocationRelativeTo(null); / Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 200);frame.setVisible(true);class Listener implements ActionListener public void actionPerformed(ActionEvent e) System.out.println(new ja
15、va.util.Date(e.getWhen();if (e.getSource() = Add)double a1=Double.parseDouble(one.getText(); double b1=Double.parseDouble(second.getText(); all.setText(String.format("%.2f",a1+b1);else if (e.getSource() = Subtract)double a1=Double.parseDouble(one.getText(); double b1=Double.parseDouble(sec
16、ond.getText(); all.setText(String.format("%.2f",a1-b1);else if (e.getSource() = Multiply)double a1=Double.parseDouble(one.getText(); double b1=Double.parseDouble(second.getText(); all.setText(String.format("%.2f",a1*b1);else if (e.getSource() = Divide)double a1=Double.parseDouble
17、(one.getText(); double b1=Double.parseDouble(second.getText(); all.setText(String.format("%.2f",a1/b1);【實(shí)驗(yàn)過程及運(yùn)行結(jié)果】在實(shí)驗(yàn)過程中遇到問題是,經(jīng)常忘記添加接口或者繼承。3、(創(chuàng)建一個(gè)投資值計(jì)算器)編寫一個(gè)程序,計(jì)算投資值在給定利率以及投資年限下的未來值。計(jì)算公式參見編程練習(xí)16.5(P456)。當(dāng)用戶點(diǎn)擊按鈕時(shí)在文本域顯示未來的總額?!緦?shí)驗(yàn)預(yù)習(xí)】1.給定Calculate問題描述:這道題是創(chuàng)建一個(gè) GUI窗口,添加組建如圖,通過點(diǎn)擊按鈕,計(jì)算 未來收益。輸入:在前面
18、三個(gè)文本域中分別寫入投資總額,投資年限,年利率。過程: 獲取文本域的內(nèi)容,計(jì)算未來收益。輸出:將未來收益額顯示在文本域中。技術(shù)支持:用到了 GUI基本組件【附編程代碼】 im護(hù)a import jaVp.awt.FlowLayout;mpOrt |java.awt.GridLayout; import java.awt.eve nt.Acti on Eve nt; import java.awt.eve nt.Actio nListe ner;import javax.swi ng. JButt on;import javax.sw in g.JFrame; import javax.sw i
19、n g.JLabel; import javax.swi ng.J Pa nel; import javax.swi ng.J TextField; import javax.swi ng.Swi ngCon sta nts;public class in vesti ngCalculator exte nds JFrame/創(chuàng)建一個(gè)名為計(jì)算投資質(zhì)的按鈕private JButton calculate=new JButton("計(jì)算投資值");/創(chuàng)建投資總額、投資年限、年利率、未來收益值的文本域private JTextField in vestme ntam ount=
20、new JTextField();private JTextField year二new JTextField();private JTextField annu allyl nterstRate=new JTextField();private JTextField futureValue=new JTextField();in vest in gCalculator() UL山/將標(biāo)簽和文本域放入面板tp1中AIJPanel p1=new jpJ*el(new GjdLayout(5,2);f'|tp1.add(new JLabel("投資總額");p1.add
21、(i nvestme ntam oun t);2.3.va.awt.BorderLayout;,布局管理器,事件監(jiān)聽等。idLayout(5,2);investmentamount.setHorizontalAlignment(SwingConstants.RIGHT);p1.add(new JLabel(" 投資年限 ");p1.add(year); year.setHorizontalAlignment(SwingConstants.RIGHT); p1.add(new JLabel(" 年利率 ");p1.add(annuallyInterstRa
22、te);annuallyInterstRate.setHorizontalAlignment(SwingConstants.RIGHT);p1.add(new JLabel(" 未來收益值 ");p1.add(futureValue); futureValue.setHorizontalAlignment(SwingConstants.RIGHT); futureValue.setEditable(false);/ 將按鈕放在面板 p2 中JPanel p2=new JPanel(new FlowLayout(FlowLayout.RIGHT);p2.add(calcula
23、te);/ 將面板 p1,p2 放到框架 frame 里add(p1,BorderLayout.CENTER);add(p2,BorderLayout.SOUTH);/ 創(chuàng)建一個(gè)監(jiān)聽器并注冊到按鈕calculate.addActionListener(new ActionListener()Override/ 實(shí)現(xiàn)點(diǎn)擊按鈕計(jì)算出未來收益并顯示在文本域中public void actionPerformed(ActionEvent e) / TODO Auto-generated methodstub if(e.getSource()=calculate)doubleamount=Double.
24、parseDouble(investmentamount.getText();double yearlimit=Double.parseDouble(year.getText(); doublerate=Double.parseDouble(annuallyInterstRate.getText();futureValue.setText(String.format("%.2f",amount*Math.pow(1+rate/1200), yearlimit*12););/ 主方法public static void main(Stringargs)investingCal
25、culator frame=new investingCalculator(); frame.setTitle(" 投資值計(jì)算器 ");frame.setSize(500,300);frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);【實(shí)驗(yàn)過程及運(yùn)行結(jié)果】【思考題】1、在上述練習(xí)中,你是否使用了匿名內(nèi)部類來實(shí)現(xiàn)監(jiān)聽器類?如果是,說明你 是如何使用的(可以使用代碼片段)?如果不是,那么使用匿名內(nèi)部類如何實(shí)現(xiàn)監(jiān)聽器類呢?有。calculate.addActi on Liste ner(new Acti on Liste ner()Override/實(shí)現(xiàn)點(diǎn)擊按鈕計(jì)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國銻摻雜二氧化錫氧化錫行業(yè)運(yùn)行動(dòng)態(tài)及發(fā)展前景分析報(bào)告
- 2025-2030年中國酸奶冰淇淋市場運(yùn)行狀況與投資戰(zhàn)略研究報(bào)告
- 2025-2030年中國造船板市場運(yùn)行動(dòng)態(tài)規(guī)劃分析報(bào)告
- 2025-2030年中國軟體家具制造行業(yè)發(fā)展現(xiàn)狀及前景趨勢分析報(bào)告
- 2025-2030年中國超高分子量聚乙烯纖維行業(yè)競爭策略規(guī)劃研究報(bào)告
- 2025-2030年中國負(fù)載均衡器行業(yè)運(yùn)行態(tài)勢及發(fā)展趨勢分析報(bào)告
- 2025-2030年中國航空飛行培訓(xùn)行業(yè)十三五規(guī)劃與投資戰(zhàn)略研究報(bào)告
- 2025-2030年中國糖醇市場運(yùn)營狀況及發(fā)展前景分析報(bào)告
- 2025-2030年中國米香型酒市場發(fā)展現(xiàn)狀及前景趨勢分析報(bào)告
- 2025-2030年中國磷石膏行業(yè)發(fā)展?jié)摿拔磥碲厔蓊A(yù)測報(bào)告
- 高三一?!吧媾c強(qiáng)弱關(guān)系思辨”審題立意及范文
- 2025年茂名市高三年級第一次綜合測試(一模)物理試卷(含答案)
- 《電子商務(wù)法律法規(guī)》電子商務(wù)專業(yè)全套教學(xué)課件
- 《產(chǎn)后出血預(yù)防與處理指南(2023)》解讀課件
- 全套教學(xué)課件《工程倫理學(xué)》
- 江蘇省建筑與裝飾工程計(jì)價(jià)定額(2014)電子表格版
- 清華大學(xué)考生自述
- 幼兒園中班繪本:《我喜歡我的小毯子》
- 法社會(huì)學(xué)(上課)
- 沉積學(xué)與古地理
- 化工原理課程設(shè)計(jì)(換熱器)
評論
0/150
提交評論