




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、計(jì)算器模擬程序的 Java 代碼import java.awt.Button;import java.awt.Frame;import java.awt.GridLayout;import java.awt.Panel;import java.awt.*;import java.awt.event.*;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.*;public class Cal extends JFrameGridLayout gl3;Panel p0, p3;JText
2、Field tf1;JButton b7, b8, b9, b10, b12,b13, b14,b15, b17, b18, b19, b20, b22, b24, b25, b26;StringBuffer str;/ double x, y;/ x double sum=0;/ double mul=1;/ double sub=0;/顯示屏所顯示的字符串和 y 都是運(yùn)算數(shù)加和乘積減差 令其為輸入的第一個(gè)值double dev;/ 除商 令其為輸入的第一個(gè)值 boolean tag= true;boolean tag1= true;int z = -1,k;/ Z表示單擊了那一個(gè)運(yùn)算符 .
3、0 表示 "+",1 表示"-",2 表示 "*",3 表示"/"/static double m ;/ 記憶的數(shù)字public Cal()Container con=getContentPane(); con.setLayout(new FlowLayout(); gl3 = new GridLayout(4, 4, 15, 15);tf1 = new JTextField(30);/顯示屏tf1.setHorizontalAlignment(JTextField.RIGHT); tf1.setEnabled(f
4、alse); tf1.setEditable(false);tf1.setText("0");tf1.addKeyListener(new Tt();b7 = new JButton("7"); b7.setForeground(Color.blue); b7.addActionListener(new Bt();b8 = new JButton("8"); b8.setForeground(Color.blue); b8.addActionListener(new Bt();b9 = new JButton("9"
5、;); b9.setForeground(Color.blue); b9.addActionListener(new Bt();b10 = new JButton("/");b10.setForeground(Color.red);b10.addActionListener(new Bt();b12 = new JButton("4"); b12.setForeground(Color.blue); b12.addActionListener(new Bt();b13 = new JButton("5"); b13.setForegr
6、ound(Color.blue); b13.addActionListener(new Bt();b14 = new JButton("6"); b14.setForeground(Color.blue); b14.addActionListener(new Bt();b15 = new JButton("*"); b15.setForeground(Color.red); b15.addActionListener(new Bt();b17 = new JButton("1"); b17.setForeground(Color.bl
7、ue); b17.addActionListener(new Bt();b18 = new JButton("2"); b18.setForeground(Color.blue); b18.addActionListener(new Bt();b19 = new JButton("3"); b19.setForeground(Color.blue); b19.addActionListener(new Bt(); b20 = new JButton("-"); b20.setForeground(Color.red); b20.add
8、ActionListener(new Bt();b22 = new JButton("0"); b22.setForeground(Color.blue); b22.addActionListener(new Bt();b24 = new JButton("."); b24.setForeground(Color.blue); b24.addActionListener(new Bt();b25 = new JButton("+"); b25.setForeground(Color.red); b25.addActionListene
9、r(new Bt();b26 = new JButton("="); b26.setForeground(Color.red); b26.addActionListener(new Bt();/ 實(shí)例化四個(gè)面板 p0 = new Panel();/p1 = new Panel();/p2 = new Panel(); p3 = new Panel();/p4 = new Panel();/ 創(chuàng)建一個(gè)空字符串緩沖區(qū)str = new StringBuffer(); con.add(p0);con.add(p3);/ 添加面板 p0 中的組件和設(shè)置其在框架中的位置和大小 p0.
10、add(tf1);p0.setBounds(10, 40, 280, 40);/ 添加面板 p1 中的組件和設(shè)置其在框架中的位置和大小/ 添加面板 p2 中的組件并設(shè)置其的框架中的位置和大小 /p2.setLayout(gl2);/p2.add(b3);/p2.add(b4);/p2.add(b5);/p2.add(b6);/p2.setBounds(10, 110, 40, 150);/ 添加面板 p3 中的組件并設(shè)置其在框架中的位置和大小 p3.setLayout(gl3);/設(shè)置 p3 的布局p3.add(b7);p3.add(b8);p3.add(b9);p3.add(b10); /p
11、3.add(b11);p3.add(b12);p3.add(b13);p3.add(b14);p3.add(b15);/p3.add(b16);p3.add(b17);p3.add(b18);p3.add(b19);p3.add(b20);/p3.add(b21);p3.add(b22);/p3.add(b23);p3.add(b24);p3.add(b25);p3.add(b26);p3.setBounds(10, 100, 280, 150);/ 添加面板 p4 中的組件和設(shè)置其在框架中的位置和大小/ 設(shè)置框架中的布局為空布局并添加 4 個(gè)面板 setLayout(null);add(p0
12、);add(p3);/add(p4);setTitle(" 計(jì)算器 ");setResizable(false);/ 禁止調(diào)整框架的大小/ 匿名類關(guān)閉窗口 addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e1) System.exit(0););setBackground(Color.lightGray); setBounds(100, 100, 310, 300);setVisible(true);class Bt implements ActionListener/z
13、 :0 表示"+",1 表示"-",2 表示"*",3 表示"/" public double ttt( )switch( z )case 0:sum = sum + y; y = sum; break;case 1:sub = sub - y; y = sub; break;case 2:mul = mul * y; y = mul; break;case 3:dev = dev / y; y = dev; break;return y;public void actionPerformed(ActionEven
14、t e2)try 單擊加號(hào)按鈕獲得 xif (e2.getSource() = b25)/ 的值和 z 的值并清空 y 的值if ( z = 0 | z = -1 )x = y;sum = sum + x;elsesum = this.ttt( );str.setLength(0);/ 清空緩沖區(qū)以便接收新的另 一個(gè)運(yùn)算數(shù)tf1.setText("" + sum);z = 0;else if (e2.getSource() = b20)/單擊減號(hào)按鈕獲得 x 的值和 z 的值并清空 y 的值if ( z = 1 | z = -1 )x = y;if(tag = true)
15、sub = x; tag = false;elsesub = sub - x;elsesub = this.ttt( );str.setLength(0);z = 1;tf1.setText("" + sub);單擊乘號(hào)按鈕獲else if (e2.getSource() = b15)/ 得 x 的值和 z 的值并清空 y 的值if(z = 2 |z = -1)x = y; mul = mul * x; else mul = this.ttt( ); str.setLength(0); tf1.setText("" + mul); z = 2;else
16、if (e2.getSource() = b10)/單擊除號(hào)按鈕獲得 x 的值和 z 的值并空 y 的值if(z = 3 |z = -1) x = y; if(tag1 = true) dev = x; tag1 = false;else if(x=0) tf1.setText(" 除數(shù)不能為零 "); elsedev = dev / x;else dev = this.ttt( );str.setLength(0); tf1.setText("" + dev); z = 3;else if (e2.getSource() = b26)/ 單擊等號(hào)按鈕輸
17、 出計(jì)算結(jié)果 str.setLength(0); switch (z) case 0:tf1.setText("" + (sum + y); y =Double.parseDouble(tf1.getText().trim();sum=0; break;case 1: tf1.setText("" + (sub -y); y =Double.parseDouble(tf1.getText().trim(); sub=0; tag = true; break;case 2: tf1.setText("" + (mul * y); y =
18、Double.parseDouble(tf1.getText().trim();mul=0;break;case 3:if (y = 0) tf1.setText(" 除數(shù)不能為零 "); else tf1.setText("" + (dev / y);y =Double.parseDouble(tf1.getText().trim();dev=0;tag1 = true;break;else if (e2.getSource() = b24)/單擊 "." 按鈕輸入小數(shù)if (tf1.getText().trim().indexOf
19、('.') !=-1)/ 判斷字符串中是否已經(jīng)包含了小數(shù)點(diǎn)else/ 如果沒數(shù)點(diǎn)有小if (tf1.getText().trim().equals("0")/如果初時(shí)顯示為 0str.setLength(0);/ System.out.println(str);tf1.setText(str.append("0" +e2.getActionCommand().toString();else if(tf1.getText().trim().equals("")/ 如果初時(shí)顯示為空則不做 任何操作elsetf1.setTe
20、xt(str.append(e2.getActionCommand().toString();y = 0d;else/ 選擇的是其他的按鈕if (e2.getSource() = b22)/ 如果選擇的是 "0" 這個(gè)數(shù)字鍵if (tf1.getText().trim().equals("0")/如果顯示屏顯示的為零不做操作 elsetf1.setText(str.append(e2.getActionCommand().toString();y =Double.parseDouble(tf1.getText().trim();/else if (e2.getSource() = b0)/ 選擇的是“ BackSpacW'按鈕/if(!tf1.getText().trim().equals("0") )/如果顯示屏顯示的不是J | A零/ if (str.length() != 1) / tf1.setText(str.delete(str.length()- 1,/str.length().toString();/ else / tf1.setText("0");/ str.setLength(0);/ / y =Double.parseDouble(tf1
溫馨提示
- 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年面紗項(xiàng)目投資可行性研究分析報(bào)告
- 睿晶半導(dǎo)體(寧波)有限公司介紹企業(yè)發(fā)展分析報(bào)告
- 摩托車轉(zhuǎn)向燈與倒車燈檢修考核試卷
- 豪華玻璃洗手盆行業(yè)深度研究報(bào)告
- 光伏電站安全生產(chǎn)標(biāo)準(zhǔn)化達(dá)標(biāo)自查報(bào)告
- 刀剪產(chǎn)品的創(chuàng)新與發(fā)展趨勢考核試卷
- 防銹劑項(xiàng)目可行性研究報(bào)告參考模板x
- 2025年中國家居家裝電商行業(yè)市場前景預(yù)測及投資戰(zhàn)略研究報(bào)告
- 期貨市場資產(chǎn)配置服務(wù)考核試卷
- 中國硅酸鹽旋窯水泥項(xiàng)目投資可行性研究報(bào)告
- 2024年山東工程職業(yè)技術(shù)大學(xué)單招職業(yè)傾向性測試題庫(500題)含答案解析
- 生活垃圾我知道(課件)二年級(jí)下冊勞動(dòng)
- 事業(yè)單位考試職業(yè)能力傾向測驗(yàn)(醫(yī)療衛(wèi)生類E類)試卷及答案指導(dǎo)
- 每日系列-計(jì)算小紙條-3年級(jí)下冊
- 2024年廣西區(qū)公務(wù)員考試《行測》真題及答案解析
- 化工安全 教案 第三章 燃燒與爆炸理論基礎(chǔ)
- 第二單元 社會(huì)主義制度的建立與社會(huì)主義建設(shè)的探索(單元解讀)- 八年級(jí)歷史下冊同步備課系列
- 新能源汽車維護(hù)與故障診斷課件 項(xiàng)目一 安全防護(hù)知識(shí)與應(yīng)用
- 闌尾炎的護(hù)理查房腹腔鏡
- 大學(xué)輔導(dǎo)員崗位考核參考指標(biāo)
- 學(xué)校實(shí)驗(yàn)室危險(xiǎn)化學(xué)品安全工作檢查記錄表
評(píng)論
0/150
提交評(píng)論