JAVA編寫(xiě)的計(jì)算器源代碼_第1頁(yè)
JAVA編寫(xiě)的計(jì)算器源代碼_第2頁(yè)
JAVA編寫(xiě)的計(jì)算器源代碼_第3頁(yè)
JAVA編寫(xiě)的計(jì)算器源代碼_第4頁(yè)
JAVA編寫(xiě)的計(jì)算器源代碼_第5頁(yè)
已閱讀5頁(yè),還剩5頁(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、精選優(yōu)質(zhì)文檔-傾情為你奉上package jsq;import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;im

2、port javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.UIManager;public class Test implements ActionListener /* * */JFrame f = new JFrame();JTe

3、xtField tf = new JTextField("0.");JMenuBar bar=new JMenuBar();JMenu menuEdit=new JMenu("編輯(E)");JMenu menuView=new JMenu("查看(V)");JMenu menuHelp=new JMenu("幫助(H)");JMenuItem menuEditCopy=new JMenuItem("復(fù)制(C)");JMenuItem menuEditPaste=new JMenuItem(&q

4、uot;粘貼(V)");JMenuItem menuHelpAbout = new JMenuItem("關(guān)于計(jì)算器(A)"); JPanel panel0 = new JPanel();JPanel panel2 = new JPanel();JPanel panel = new JPanel();JPanel panel3 = new JPanel();JPanel panel4 = new JPanel();JPanel panel5 = new JPanel();JButton btBk = new JButton(" Backspace&quo

5、t;);JButton btCe = new JButton("Ce");JButton btCl = new JButton("C");JButton bt7 = new JButton("7");JButton bt8 = new JButton("8");JButton bt9 = new JButton("9");JButton btDiv = new JButton("/");JButton btSqrt = new JButton("sqrt"

6、);JButton bt4 = new JButton("4");JButton bt5 = new JButton("5");JButton bt6 = new JButton("6");JButton btMult = new JButton("*");JButton btMol = new JButton(" % ");JButton bt1 = new JButton("1");JButton bt2 = new JButton("2");JBut

7、ton bt3 = new JButton("3");JButton btMinu = new JButton("-");JButton btCoun = new JButton(" 1/x");JButton bt0 = new JButton("0");JButton btPoint = new JButton(".");JButton btEqual = new JButton("=");JButton btAdd = new JButton("+"

8、);JButton btSign = new JButton(" +/-");/* * param args */public void init()f.add(panel0);f.add(tf,"North");f.setJMenuBar(bar);bar.add(menuEdit);bar.add(menuView);bar.add(menuHelp);menuEdit.add(menuEditCopy);menuEdit.add(menuEditPaste);menuHelp.add(menuHelpAbout); tf.setEditable(f

9、alse);/不可編輯tf.setHorizontalAlignment(JTextField.RIGHT);/從右側(cè)輸入數(shù)據(jù)panel0.add(panel2);panel0.add(panel);panel0.add(panel3);panel0.add(panel4);panel0.add(panel5);panel2.setLayout(new GridLayout(1,4);panel2.add(btBk);panel2.add(btCe);panel2.add(btCl);panel.setLayout(new GridLayout(1,5);panel.add(bt7);pane

10、l.add(bt8);panel.add(bt9);panel.add(btDiv);panel.add(btSqrt);/根號(hào)panel3.setLayout(new GridLayout(1,5);panel3.add(bt4);panel3.add(bt5);panel3.add(bt6);panel3.add(btMult);panel3.add(btMol);/百分號(hào)panel4.setLayout(new GridLayout(1,5);panel4.add(bt1);panel4.add(bt2);panel4.add(bt3);panel4.add(btMinu);panel4

11、.add(btCoun);/倒數(shù)panel5.setLayout(new GridLayout(1,5);panel5.add(bt0);panel5.add(btPoint);panel5.add(btAdd);panel5.add(btEqual);panel5.add(btSign);/正負(fù)符號(hào)btBk.setForeground(new Color(255, 0, 0);btCe.setForeground(new Color(255, 0, 0);btCl.setForeground(new Color(255, 0, 0);btAdd.setForeground(new Color

12、(255, 0, 0);btMult.setForeground(new Color(255, 0, 0);btDiv.setForeground(new Color(255, 0, 0);btMinu.setForeground(new Color(255, 0, 0);btEqual.setForeground(new Color(255, 0, 0);bt0.setForeground(new Color(0,0,255);bt1.setForeground(new Color(0, 0, 255);bt2.setForeground(new Color(0,0,255);bt3.set

13、Foreground(new Color(0,0,255);bt4.setForeground(new Color(0,0,255);bt5.setForeground(new Color(0,0,255);bt6.setForeground(new Color(0,0,255);bt7.setForeground(new Color(0,0,255);bt8.setForeground(new Color(0,0,255);bt9.setForeground(new Color(0,0,255);btSqrt.setForeground(new Color(0,0,255);btMol.se

14、tForeground(new Color(0,0,255);btCoun.setForeground(new Color(0,0,255);btSign.setForeground(new Color(0,0,255);/注冊(cè)監(jiān)聽(tīng)bt1.addActionListener(this);bt2.addActionListener(this);bt3.addActionListener(this);bt4.addActionListener(this);bt5.addActionListener(this);bt6.addActionListener(this);bt7.addActionLis

15、tener(this);bt8.addActionListener(this);bt9.addActionListener(this);bt0.addActionListener(this);btAdd.addActionListener(this);btMinu.addActionListener(this);btMult.addActionListener(this);btDiv.addActionListener(this);btEqual.addActionListener(this);btPoint.addActionListener(this);btSqrt.addActionLi

16、stener(this);btMol.addActionListener(this);btCoun.addActionListener(this);btSign.addActionListener(this);btBk.addActionListener(this);btCe.addActionListener(this);btCl.addActionListener(this);menuEditCopy.addActionListener(this);menuEditPaste.addActionListener(this);menuHelpAbout.addActionListener(t

17、his);f.addWindowListener(new MyWinLis();f.setTitle("計(jì)算器");f.setSize(295,240);f.setResizable(false);/不能改變窗體大小f.setVisible(true);String foreNum=null;String currOpr=null;String currNum=null; boolean i = true; String copy;/復(fù)制public void actionPerformed(ActionEvent e) / TODO 自動(dòng)生成方法存根Object obj=

18、e.getSource();/輸入數(shù)字if(obj=bt1|obj=bt2|obj=bt3|obj=bt4|obj=bt5|obj=bt6|obj=bt7|obj=bt8|obj=bt9|obj=bt0)if(i=true)tf.setText(e.getActionCommand();else tf.setText(tf.getText()+e.getActionCommand();i = false;/小數(shù)點(diǎn)else if(obj=btPoint)if(i=false) tf.setText(tf.getText()+'.'); /實(shí)現(xiàn)開(kāi)根號(hào)else if(obj=btSq

19、rt)if(tf.getText().charAt(0)='-')tf.setText("函數(shù)輸入無(wú)效"); elsetf.setText(Double.toString(java.lang.Math.sqrt(Double. parseDouble(tf.getText(); i = true;/實(shí)現(xiàn)倒數(shù)else if(obj=btCoun)if (tf.getText().charAt(0) = '0') tf.setText("零不能求倒數(shù)");i = true; else String s = Double.toS

20、tring(1 / Double.parseDouble(tf.getText(); foreNum = tf.getText(); tf.setText(s); i = true; /實(shí)現(xiàn)+/-符號(hào) else if (obj = btSign) boolean isNumber = true;String s = tf.getText(); for (int m = 0; m< s.length(); m+) if (! (s.charAt(m) >= '0' && s.charAt(m) <='9' | s.charAt(m

21、) ='.'| s.charAt(m) = '-') break; if (isNumber = true) /如果當(dāng)前字符串首字母有-號(hào),代表現(xiàn)在是個(gè)負(fù)數(shù),再按下時(shí),則將首符號(hào)去掉 if (s.charAt(0) = '-') tf.setText(""); for (int m = 1; m< s.length(); m+) char a = s.charAt(m); tf.setText(tf.getText() + a); /如果當(dāng)前字符串第一個(gè)字符不是符號(hào),則添加一個(gè)符號(hào)在首字母處 else tf.setTex

22、t('-' + s); /實(shí)現(xiàn)%功能else if(obj=btMol)boolean isNumber = true;if(isNumber=true)tf.setText(String.valueOf(Double.parseDouble(tf.getText()/100);elsetf.setText("錯(cuò)誤");/實(shí)現(xiàn)符號(hào)運(yùn)算else if(obj=btAdd|obj=btMinu|obj=btDiv|obj=btMult)if(i=false)currNum=tf.getText();if(currOpr="+")tf.setT

23、ext(String.valueOf(Double.parseDouble(foreNum)+Double.parseDouble(currNum);else if(currOpr="-")tf.setText(String.valueOf(Double.parseDouble(foreNum)-Double.parseDouble(currNum);else if(currOpr="*")tf.setText(String.valueOf(Double.parseDouble(foreNum)*Double.parseDouble(currNum);e

24、lse if(currOpr="/")if (Double.parseDouble(tf.getText() = 0) tf.setText("除數(shù)不能為零"); else tf.setText(String.valueOf(Double.parseDouble(foreNum)/Double.parseDouble(currNum);foreNum=tf.getText();tf.setText("");currOpr=e.getActionCommand();i = false;/實(shí)現(xiàn)等號(hào)else if(obj=btEqual)c

25、urrNum=tf.getText();if(currOpr="+")tf.setText(String.valueOf(Double.parseDouble(foreNum)+Double.parseDouble(currNum);else if(currOpr="-")tf.setText(String.valueOf(Double.parseDouble(foreNum)-Double.parseDouble(currNum);else if(currOpr="*")tf.setText(String.valueOf(Doubl

26、e.parseDouble(foreNum)*Double.parseDouble(currNum);else if(currOpr="/")if (Double.parseDouble(tf.getText() = 0) tf.setText("除數(shù)不能為零"); elsetf.setText(String.valueOf(Double.parseDouble(foreNum)/Double.parseDouble(currNum);currOpr=e.getActionCommand();i = true;/實(shí)現(xiàn)Backspace功能else if(

27、obj=btBk)String s = tf.getText();tf.setText("");for (int m = 0;m <s.length() - 1; m+) char a = s.charAt(m); tf.setText(tf.getText() + a); i = true;/實(shí)現(xiàn)Ce功能else if (obj = btCe) tf.setText("0."); /實(shí)現(xiàn)C功能else if (obj = btCl) foreNum=currNum="" tf.setText("0.");/實(shí)現(xiàn)復(fù)制else if(obj=menuEditCopy)copy=tf.getText();/實(shí)現(xiàn)粘貼else if (obj = menuEditPaste) tf.setText(copy);

溫馨提示

  • 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)論