![實驗報告 編寫一個簡易實用的計算器_第1頁](http://file1.renrendoc.com/fileroot_temp2/2020-8/5/d78a06a9-bb42-4e4f-90f9-e577b9affb63/d78a06a9-bb42-4e4f-90f9-e577b9affb631.gif)
![實驗報告 編寫一個簡易實用的計算器_第2頁](http://file1.renrendoc.com/fileroot_temp2/2020-8/5/d78a06a9-bb42-4e4f-90f9-e577b9affb63/d78a06a9-bb42-4e4f-90f9-e577b9affb632.gif)
![實驗報告 編寫一個簡易實用的計算器_第3頁](http://file1.renrendoc.com/fileroot_temp2/2020-8/5/d78a06a9-bb42-4e4f-90f9-e577b9affb63/d78a06a9-bb42-4e4f-90f9-e577b9affb633.gif)
![實驗報告 編寫一個簡易實用的計算器_第4頁](http://file1.renrendoc.com/fileroot_temp2/2020-8/5/d78a06a9-bb42-4e4f-90f9-e577b9affb63/d78a06a9-bb42-4e4f-90f9-e577b9affb634.gif)
![實驗報告 編寫一個簡易實用的計算器_第5頁](http://file1.renrendoc.com/fileroot_temp2/2020-8/5/d78a06a9-bb42-4e4f-90f9-e577b9affb63/d78a06a9-bb42-4e4f-90f9-e577b9affb635.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、開課實驗室:現(xiàn)代信息交流中心40* 開課時間: 2012年*月*日 實驗報告: 年 月 日 學(xué)院名稱 年級、專業(yè)、班 學(xué)號 姓名 同組姓名無課程名稱Java程序設(shè)計實驗項目名稱四、Java圖形用戶界面開發(fā)指導(dǎo)教師尉斌實驗類型驗證 綜合 設(shè)計 創(chuàng)新 成績教師評語 教師簽名: 年 月 日一、 實驗?zāi)康模菏煜ava圖形用戶界面的開發(fā)原理與技術(shù),包括各種組件和容器技術(shù)。二、 實驗內(nèi)容:編寫一個簡易實用的計算器。三、 源代碼清單:package exp3;import java.applet.Applet;import java.awt.BorderLayout;import java.awt.But
2、ton;import java.awt.Color;import java.awt.GridLayout;import java.awt.Panel;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;class Result extends Panel TextField tf;Result() tf = new TextField(0, 25);add(tf);class Key extends Panel Button backSpace, cle
3、arEnter, cancel,lBracket,rBracket;Button one, two, three, four, five, six, seven, eight, nine, zero;Button add, subtract, multiplay, divide;Button sqrt, reciprocal, mod;Button positiveAndNegative, point, equal;Key() setLayout(new GridLayout(5, 5);backSpace = new Button();backSpace.setForeground(new
4、Color(255, 0, 0);add(backSpace);clearEnter = new Button(CE);clearEnter.setForeground(new Color(255, 0, 0);add(clearEnter);cancel = new Button(C);cancel.setForeground(new Color(255, 0, 0);add(cancel); lBracket =new Button();add(lBracket);rBracket =new Button();add(rBracket);seven = new Button(7);add(
5、seven);eight = new Button(8);add(eight);nine = new Button(9);add(nine);divide = new Button(/);add(divide);sqrt = new Button(sqrt);add(sqrt);four = new Button(4);add(four);five = new Button(5);add(five);six = new Button(6);add(six);multiplay = new Button(*);add(multiplay);mod = new Button(%);add(mod)
6、;one = new Button(1);add(one);two = new Button(2);add(two);three = new Button(3);add(three);subtract = new Button(-);add(subtract);reciprocal = new Button(1/x);add(reciprocal);zero = new Button(0);add(zero);positiveAndNegative = new Button(+/-);add(positiveAndNegative);point = new Button(.);add(poin
7、t);add = new Button(+);add(add);equal = new Button(=);add(equal);public class Calculator extends Applet implements ActionListener Result result = new Result();Key key = new Key();boolean isInit = true; String operation = ; float theFirstNum = 0; float theSecondNum = 0; float f = 0;/ 用于括號計算時的使用 Strin
8、g s=;/ 用于括號計算時的使用public void init() setLayout(new BorderLayout();add(North, result); add(Center, key); key.add.addActionListener(this);key.divide.addActionListener(this);key.eight.addActionListener(this);key.equal.addActionListener(this);key.five.addActionListener(this);key.four.addActionListener(th
9、is);key.mod.addActionListener(this);key.multiplay.addActionListener(this);key.nine.addActionListener(this);key.one.addActionListener(this);key.point.addActionListener(this);key.positiveAndNegative.addActionListener(this);key.reciprocal.addActionListener(this);key.seven.addActionListener(this);key.si
10、x.addActionListener(this);key.sqrt.addActionListener(this);key.subtract.addActionListener(this);key.three.addActionListener(this);key.two.addActionListener(this);key.zero.addActionListener(this);key.backSpace.addActionListener(this);key.cancel.addActionListener(this);key.clearEnter.addActionListener
11、(this);key.lBracket.addActionListener(this);key.rBracket.addActionListener(this);public void actionPerformed(ActionEvent e) if (e.getSource() = key.one) if (isInit) result.tf.setText(1); isInit = false; else result.tf.setText(result.tf.getText() + 1);if (e.getSource() = key.two) if (isInit) result.t
12、f.setText(2);isInit = false; else result.tf.setText(result.tf.getText() + 2);if (e.getSource() = key.three) if (isInit) result.tf.setText(3);isInit = false; else result.tf.setText(result.tf.getText() + 3);if (e.getSource() = key.four) if (isInit) result.tf.setText(4);isInit = false; else result.tf.s
13、etText(result.tf.getText() + 4);if (e.getSource() = key.five) if (isInit) result.tf.setText(5);isInit = false; else result.tf.setText(result.tf.getText() + 5);if (e.getSource() = key.six) if (isInit) result.tf.setText(6);isInit = false; else result.tf.setText(result.tf.getText() + 6);if (e.getSource
14、() = key.seven) if (isInit) result.tf.setText(7);isInit = false; else result.tf.setText(result.tf.getText() + 7);if (e.getSource() = key.eight) if (isInit) result.tf.setText(8);isInit = false; else result.tf.setText(result.tf.getText() + 8);if (e.getSource() = key.nine) if (isInit) result.tf.setText
15、(9);isInit = false; else result.tf.setText(result.tf.getText() + 9);if (e.getSource() = key.zero) if(result.tf.getText().equals(0) else result.tf.setText(result.tf.getText() + 0);if (e.getSource() = key.point) if (result.tf.getText().indexOf(.) = -1) result.tf.setText(result.tf.getText() + .);isInit
16、 = false; if (e.getSource() = key.add) theFirstNum = Float.parseFloat(result.tf.getText();operation = add;isInit = true;if (e.getSource() = key.subtract) theFirstNum = Float.parseFloat(result.tf.getText();operation = subtract;isInit = true;if (e.getSource() = key.multiplay) theFirstNum = Float.parse
17、Float(result.tf.getText();operation = multiplay;isInit = true;if (e.getSource() = key.divide) theFirstNum = Float.parseFloat(result.tf.getText();operation = divide;isInit = true;if (e.getSource() = key.mod) theFirstNum = Float.parseFloat(result.tf.getText();operation = mod;isInit = true;if (e.getSou
18、rce() = key.sqrt) theFirstNum = Float.parseFloat(result.tf.getText();result.tf.setText(String.valueOf(Math.sqrt(theFirstNum);isInit = true;if (e.getSource() = key.reciprocal) if (result.tf.getText().equals(0)result.tf.setText(Error);else theFirstNum = Float.parseFloat(result.tf.getText();result.tf.s
19、etText(String.valueOf(1 / theFirstNum);isInit = true; if (e.getSource() = key.positiveAndNegative) if (result.tf.getText().equals(0) else if (result.tf.getText().startsWith(-)result.tf.setText(result.tf.getText().substring(1);elseresult.tf.setText(- + result.tf.getText();if (e.getSource() = key.equa
20、l) theSecondNum = Float.parseFloat(result.tf.getText();if (operation.equals() result.tf.setText(0);if (operation.equals(add)result.tf.setText(String.valueOf(theFirstNum + theSecondNum);if (operation.equals(subtract)result.tf.setText(String.valueOf(theFirstNum - theSecondNum);if (operation.equals(mul
21、tiplay)result.tf.setText(String.valueOf(theFirstNum * theSecondNum);if (operation.equals(divide) if (result.tf.getText().equals(0)result.tf.setText(Error);elseresult.tf.setText(String.valueOf(theFirstNum / theSecondNum);if (operation.equals(mod) if (result.tf.getText().equals(0)result.tf.setText(Err
22、or);elseresult.tf.setText(String.valueOf(theFirstNum % theSecondNum);isInit = true; theFirstNum = 0;theSecondNum = 0;operation = ; if (e.getSource() = key.backSpace) if (result.tf.getText().equals(0)else if (result.tf.getText().length() = 1) result.tf.setText(0);isInit = true; elseresult.tf.setText(
23、result.tf.getText().substring(0,result.tf.getText().length() - 1);if (e.getSource() = key.clearEnter) result.tf.setText(0);isInit = true; if (e.getSource() = key.cancel) result.tf.setText(0);theFirstNum = 0;theSecondNum = 0;operation = ;isInit = true; if (e.getSource() = key.lBracket) f = theFirstNum; / 把第一個數(shù)賦給fs = operation; / 把運算符號賦給stheFirstNum = 0;theSecondNum = 0;operation = ;isInit = true; /回到初始化,再次輸入的數(shù)值為新的開始,但文本域內(nèi)容不變if (e.getSourc
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國航空貨運管理系統(tǒng)數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年智能中央空調(diào)節(jié)電器項目投資價值分析報告
- 2025年柱式三層架項目可行性研究報告
- 2025至2030年中國電子式軟啟動器數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國快速升溫箱式實驗電爐數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國C型窗鉸鏈數(shù)據(jù)監(jiān)測研究報告
- 2025年中國柴油濾紙市場調(diào)查研究報告
- 摩托車燈具組裝工藝與質(zhì)量檢驗考核試卷
- 小吃店收銀系統(tǒng)操作與財務(wù)管理考核試卷
- 2025-2030年戶外冒險樂園行業(yè)跨境出海戰(zhàn)略研究報告
- 四川省自貢市2024-2025學(xué)年上學(xué)期八年級英語期末試題(含答案無聽力音頻及原文)
- 通用電子嘉賓禮薄
- 技能大賽題庫(空分)
- 浙江省杭州市2022年中考語文模擬試卷24
- 污水處理廠設(shè)備的操作規(guī)程(完整版)
- 通快激光發(fā)生器trucontrol操作手冊
- GB/T 28419-2012風(fēng)沙源區(qū)草原沙化遙感監(jiān)測技術(shù)導(dǎo)則
- GB/T 22077-2008架空導(dǎo)線蠕變試驗方法
- DDI領(lǐng)導(dǎo)力-高績效輔導(dǎo)課件
- 水泥罐安裝與拆除專項施工方案
- 鋼筋工專項安全教育
評論
0/150
提交評論