科學(xué)計(jì)算器源代碼_第1頁(yè)
科學(xué)計(jì)算器源代碼_第2頁(yè)
科學(xué)計(jì)算器源代碼_第3頁(yè)
科學(xué)計(jì)算器源代碼_第4頁(yè)
科學(xué)計(jì)算器源代碼_第5頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、精選文檔package two;import java.awt.*; import javax.swing.*;import java.awt.event.*;import java.io.*;import java.util.*;public class calculator implements ActionListener int count = 0;private static int a;JFrame frame = new JFrame 計(jì)算器);JTextArea area = new JTextArea();JTextField fieldshow = new JTextFie

2、ld(0);JTextField fieldcalculator = new JTextField();JPanel leftpanel = new JPanel();JPanel rightpanel = new JPanel();JPanel buttonpanel = new JPanel();JPanel motionpanel = new JPanel();JButton button1 = new JButton(1);JButton button2 = new JButton(2);JButton button3 = new JButton(3);JButton button4

3、= new JButton(+);JButton button5 = new JButton(c);JButton button6 = new JButton(4);JButton button7 = new JButton(5);JButton button8 = new JButton(6);JButton button9 = new JButton(-);JButton butt on 10 = new JButto n(退格);JButton button11 = new JButton(7);JButton button12 = new JButton(8);JButton butt

4、on13 = new JButton(9);JButton button14 = new JButton(*);JButton button15 = new JButton(sin);JButton button16 = new JButton(0);JButton button17 = new JButton(+/-);JButton button18 = new JButton(.);JButton button19 = new JButton(/);JButton button20 = new JButton(=);JButton butt on21 = new JButto n(保存)

5、;JButton butto n22 = new JButt on(復(fù)制);JButton butto n23 = new JButto n(清除); StringBuffer S = new StringBuffer();/ t用來(lái)記錄前一個(gè)運(yùn)算符號(hào)是+ - * / =中的哪一個(gè)char t;/ count用來(lái)實(shí)現(xiàn)運(yùn)算符號(hào)計(jì)數(shù),a用來(lái)識(shí)別退格、正負(fù)號(hào)操作前一個(gè)操作是 否是運(yùn)算符號(hào)+ - * / sin =/ int count = 0, a = 0;Double x1, x2 = 0d, result;p ublic static void main( Stri ng a) calculat

6、or that = new calculator(); that.go();void go() left pan el.setLayout( new BorderLayout();left pan el.add(fieldshow, BorderLayout.NORTH);left pan el.add(butto npanel, BorderLayout.CENTER);butt onpan el.setLayout (new GridLayout(4, 5); butt onpan el.add(butt on 1);butt onpan el.add(butt on 2);butt on

7、pan el.add(butt on 3);butt onpan el.add(butt on4);butt onpan el.add(butt on 5);butt onpan el.add(butt on 6);butt onpan el.add(butt on 7);butt onpan el.add(butt on 8);butt onpan el.add(butt on 9);butt onpan el.add(butt on 10);butt onpan el.add(butt on 11);butto npan el.add(butto n12);butt onpan el.ad

8、d(butt on 13);butt onpan el.add(butt on 14);butt onpan el.add(butt on 15);butt onpan el.add(butt on 16);butt onpan el.add(butt on 17);butt onpan el.add(butt on 18);butt onpan el.add(butt on 19);butt onpan el.add(butt on 20);butt on 1.addActio nListe ner(this);/ 事件監(jiān)聽(tīng) butt on 2.addAct ion Liste ner(th

9、is);butt on 3.addAct ion Liste ner(this);butt on4 .addAct ion Liste ner(this);butt on 5.addAct ion Liste ner(this); butt on 6.addAct ion Liste ner(this); butt on 7.addAct ion Liste ner(this); butt on 8.addAct ion Liste ner(this); butt on 9.addAct ion Liste ner(this); butt on 10.addActi on Liste ner(

10、this); butt on 11.addActi on Liste ner(this); butto n12.addActio nListe ner(this); butto n13.addActio nListe ner(this); butt on 14.addActio nListe ner(this); butt on 15.addActi on Liste ner(this); butt on 16.addActi on Liste ner(this); butt on 17.addActi on Liste ner(this); butt on 18.addActi on Lis

11、te ner(this); butt on 19.addActi on Liste ner(this); butt on 20.addActio nListe ner(this); butto n21.addActio nListe ner(this); butt on 22.addActio nListe ner(this); butt on 23.addActio nListe ner(this);right pan el.setLayout (new BorderLayout();right pan el.add(fieldcalculator, BorderLayout.NORTH);

12、right pan el.add(area, BorderLayout.CENTER);right pan el.add(moti onpan el, BorderLayout.SOUTH);moti onpan el.add(butt on 21);moti onpan el.add(butt on 22);moti onpan el.add(butt on 23);Container con = frame.getC ontentPan e(); frame.setLayout (new GridLayout(1,2); con. add(left pan el);con. add(rig

13、ht pan el);frame.setBou nds(200, 200, 600, 300);frame.setVisible(true);frame.setDefaultClose Op eratio n( JFrame.EXIT_ON_CLOSE); frame.addWindowListener(new WindowListener() / 關(guān)閉程序時(shí)彈出警p ublic void win dowClos in g(Wi ndowEve nt e) in t result = JOp tio nPan e.showCo nfirmDialog(frame,確實(shí)要關(guān)閉程序嗎?,警告, J

14、Op tio nPan e.OK_CANCEL_O PTION);if (result = JOp tio nPan e.OK_CANCEL_ OP TION) System.exit(0); else p ublic void wi ndow Open ed(Wi ndowEve nt e) p ublic void wi ndowico nified(Wi ndowEve nt e) p ublic void wi ndowDeico nified(Wi ndowEve nt e) p ublic void win dowDeactivated(Wi ndowEve nt e) p ubl

15、ic void wi ndowClosed(Wi ndowEve nt e) p ublic void wi ndowActivated(Wi ndowEve nt e) );P ublic void actio nP eformed(Actio nEve nt e) / 0123456789 等按鈕if (e.getSourceO = butt on1 | e.getSource() = butt on2II e.getSourceO = butt on3 | e.getSource() = butt on6II e.getSourceO = butt on7 | e.getSource()

16、 = butt on8| e.getSourceO = butt on11 | e.getSourceO = butt on12| e.getSourceO = butt on13 | e.getSource() = butt on 16) a = 0;if (co unt = 0) S.a ppen d(e.getActio nComma nd(); fieldshow.setText(S.toStri ng();x1 = Double .p arseDouble(fieldshow.getText(); else if (co unt = 1) S.a ppen d(e.getActio

17、nComma nd(); fieldshow.setText(S.toStri ng();x2 = Double .p arseDouble(fieldshow.getText();/小數(shù)點(diǎn)的容錯(cuò)性if (e.getSourceO = butt on 18)/ 單擊.按鈕輸入小數(shù)a = 0;if (fieldshow.getText().trim().indexOf(.) != -1)/ 判斷字符串中是否已經(jīng)包含了小數(shù)點(diǎn) else/如果沒(méi)有小數(shù)點(diǎn)if (fieldshow.getText().trim().equals(0)/ 如果初時(shí)顯示為 0S.setLe ngth(0);fieldsho

18、w.setText(S.a ppen d(0 + e.getActio nComma nd() .toStri ng(); else if (fieldshow.getText().trim().equals()/ 如果初時(shí)顯示為空 則不做任何操作 else fieldshow.setText(S.a ppen d(e.getActio nComma nd().toStri ng();/運(yùn)算符號(hào)+ - * /if (e.getSource() = butt on4 | e.getSource() = butt on9| e.getSource() = butt on14 | e.getSour

19、ce() = butt on 19) coun t+;a = 1;S.setLe ngth(0);if (e.getSource() = butto n4)/ + 法switch (t) case +:result = x1 + x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + + + x2 + = + result); area.a ppen d(x1 + + + x2 + = + result + n); break;case -:result = x1 - x2;fieldshow.setText(

20、result.toStri ng(); fieldcalculator.setText(x1 + + + x2 + = + result); area.a ppen d(x1 + - + x2 + = + result + n); break;case *:result = x1 * x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + + + x2 + = + result); area.a ppen d(x1 + * + x2 + = + result + n); break;case /:if (x2

21、= 0)fieldshow.setText(除數(shù)不能為零);else result = x1 / x2;fieldshow.setText(result.toStri ng();fieldcalculator.setText(x1 + + + x2 + = + result); area.a ppen d(x1 + / + x2 + = + result + n);break;case =:default:if (count = 2) x1 = result; t = +;if (e.getSourceO = butto n9)/ -法switch (t) case +:result = x1

22、 + x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + + + x2 + = + result); area.a ppen d(x1 + + + x2 + = + result + n); break;case -:result = x1 - x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + - + x2 + = + result); area.a ppen d(x1 + - + x2 + = + result +

23、n); break;case *:result = x1 * x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + * + x2 + = + result); area.a ppen d(x1 + * + x2 + = + result + n); break;case /:if (x2 = 0)fieldshow.setText(除數(shù)不能為零);else result = x1 / x2; fieldshow.setText(result.toStri ng(); fieldcalculator.setTe

24、xt(x1 + / + x2 + = + result); area.a ppen d(x1 + / + x2 + = + result + n); break;case =:default:if (cou nt = 2) x1 = result;t =-;if (e.getSourceO = butt on 14)/ * 法 switch (t) case +:result = x1 + x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + + + x2 + = + result); area.a ppen

25、 d(x1 + + + x2 + = + result + n); break;case -:result = x1 - x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + - + x2 + = + result); area.a ppen d(x1 + - + x2 + = + result + n); break;case *:result = x1 * x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + * + x

26、2 + = + result); area.a ppen d(x1 + * + x2 + = + result + n); break;case /:if (x2 = 0)fieldshow.setText(除數(shù)不能為零);else result = x1 / x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + / + x2 + = + result); area.a ppen d(x1 + / + x2 + = + result + n); break;case =:default: if (cou nt

27、 = 2) x1 = result;1*1. t =if (e.getSourceO = butt on 19)/法 switch (t) case +:result = x1 + x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + + + x2 + = + result); area.a ppen d(x1 + + + x2 + = + result + n); break;case -:result = x1 - x2;fieldshow.setText(result.toStri ng(); fiel

28、dcalculator.setText(x1 + - + x2 + = + result); area.a ppen d(x1 + - + x2 + = + result + n); break;case *:result = x1 * x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + * + x2 + = + result); area.a ppen d(x1 + * + x2 + = + result + n); break;case /:if (x2 = 0)fieldshow.setText(除數(shù)

29、不能為零);else result = x1 / x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + / + x2 + = + result); area.a ppen d(x1 + / + x2 + = + result + n); break;case =:default:if (count = 2) x1 = result;t = /;/清零操作if (e.getSourceO = butt on5) fieldshow.setText(0);/ 把顯示屏清零count = 0;a = 0;t =;x

30、1 = 0d;x2 = 0d;S.setLe ngth(0);/清空字符串緩沖區(qū)以準(zhǔn)備接收新的輸入運(yùn)算數(shù)/退格操作if (e.getSourceO = butto n10) if (a = 1) /判斷前一個(gè)操作是否是運(yùn)算符號(hào)和等于操作System.out. println(運(yùn)算符號(hào)不能退格!); else if (!fieldshow.getText().trim().equals(0)/ 如果顯示屏顯示的不11是零if (S.length() != 1) /可能拋出字符串越界異常fieldshow.setText(S.delete(S.le ngth() - 1, S.le ngth()

31、.toStri ng(); else fieldshow.setText(0);S.setLe ngth(0);if (co unt = 0) x1 = Double .p arseDouble(fieldshow.getText().trim(); else if (co unt = 1) /x2 = Double .p arseDouble(fieldshow.getText().trim(); 求正弦值操作實(shí)現(xiàn)運(yùn)算過(guò)程中正弦值的優(yōu)先性if (e.getSourceO = butt on 15) a = 1;if (co unt = 0) if (S.length() = 0) / 第一個(gè)

32、按鍵不能為 sin fieldshow.setTextC先輸入數(shù)字); else result = Math.si n(x1); fieldshow.setText(result.toStri ng(); fieldcalculator.setText(si nC + x1 + ) + = + result); area.a ppen d(si nC + x1 + ) + = + result + n); x1 = result;/將正弦值求出來(lái)賦值給x1if (cou nt = 1) result = Math.si n(x2);fieldcalculator.setText(si nC +

33、 x2 + ) + = + result); area.a ppen d(si nC + x2 + ) + = + result + n); x2 = result;/將正弦值求出來(lái)賦值給 x2S.setLe ngth(O);/正負(fù)值操作if (e.getSourceO = butto n17) if (a = 1) /判斷前一個(gè)操作是否是運(yùn)算符號(hào)和等于操作 else if (co unt = 0) x1 = Double .p arseDouble(fieldshow.getText().trim(); fieldshow.setText( + (-x1);x1 = -x1; else if

34、 (co unt = 1) x2 = Double .p arseDouble(fieldshow.getText().trim(); fieldshow.setText(” + (-x2);x2 = -x2;/等于號(hào)操作if (e.getSource() = butto n20) switch (t) case +:result = x1 + x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + + + x2 + = + result); area.a ppen d(x1 + + + x2 + = + re

35、sult + n); break;case -:result = x1 - x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + - + x2 + = + result); area.a ppen d(x1 + - + x2 + = + result + n); break;case *:result = x1 * x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + * + x2 + = + result); area.a

36、 ppen d(x1 + * + x2 + = + result + n); break;case /:if (x2 = 0)fieldshow.setText(除數(shù)不能為零);else result = x1 / x2;fieldshow.setText(result.toStri ng(); fieldcalculator.setText(x1 + / + x2 + = + result); area.a ppen d(x1 + / + x2 + = + result + n); break;case =:default:x1 = x2 = 0d;count = 0;a = 1;t =;S

37、.setLe ngth(O);/保存操作if (e.getSource() = butto n21) JFileChooser jf = new JFileChooser();/實(shí)例化文件選擇器 JTextField jt = new JTextField();Cale ndar ca = Cale ndar.get In sta nce();int year = ca.get(Cale ndar.YEAR);/獲取年份int mon th = ca.get(Cale ndar.MONTH);/ 獲取月份int day = ca.get(Cale ndar.DATE);/獲取日int mi n

38、ute = ca.get(Cale ndar.MINUTE);/ 分int hour = ca.get(Cale ndar.HOUR);/ 小時(shí) int seco nd = ca.get(Cale ndar.SECOND);秒 int select = jf.showSaveDialog(frame);/ 顯示對(duì)話框 if (select = JFileChooser.A PP ROVE_O PTION)單擊保存按鈕 File file = jf.getSelectedFile();/ 得到選擇的文件 try Writer out = n ull;out = new FileWriter(jf.getSelectedFile();/ 輸出流Stri ng str = area.getText() + 時(shí)間:+ year + -+ mon th + - + day + n + hour + : + mi nute + : +

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論