JAVA编写的计算器源代码_第1页
JAVA编写的计算器源代码_第2页
JAVA编写的计算器源代码_第3页
JAVA编写的计算器源代码_第4页
JAVA编写的计算器源代码_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上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("复制(C)");JMenuItem menuEditPaste=new JMenuItem(&q

4、uot;粘贴(V)");JMenuItem menuHelpAbout = new JMenuItem("关于计算器(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);/从右侧输入数据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);/根号panel3.setLayout(new GridLayout(1,5);panel3.add(bt4);panel3.add(bt5);panel3.add(bt6);panel3.add(btMult);panel3.add(btMol);/百分号panel4.setLayout(new GridLayout(1,5);panel4.add(bt1);panel4.add(bt2);panel4.add(bt3);panel4.add(btMinu);panel4

11、.add(btCoun);/倒数panel5.setLayout(new GridLayout(1,5);panel5.add(bt0);panel5.add(btPoint);panel5.add(btAdd);panel5.add(btEqual);panel5.add(btSign);/正负符号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);/注册监听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("计算器");f.setSize(295,240);f.setResizable(false);/不能改变窗体大小f.setVisible(true);String foreNum=null;String currOpr=null;String currNum=null; boolean i = true; String copy;/复制public void actionPerformed(ActionEvent e) / TODO 自动生成方法存根Object obj=

18、e.getSource();/输入数字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;/小数点else if(obj=btPoint)if(i=false) tf.setText(tf.getText()+'.'); /实现开根号else if(obj=btSq

19、rt)if(tf.getText().charAt(0)='-')tf.setText("函数输入无效"); elsetf.setText(Double.toString(java.lang.Math.sqrt(Double. parseDouble(tf.getText(); i = true;/实现倒数else if(obj=btCoun)if (tf.getText().charAt(0) = '0') tf.setText("零不能求倒数");i = true; else String s = Double.toS

20、tring(1 / Double.parseDouble(tf.getText(); foreNum = tf.getText(); tf.setText(s); i = true; /实现+/-符号 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) /如果当前字符串首字母有-号,代表现在是个负数,再按下时,则将首符号去掉 if (s.charAt(0) = '-') tf.setText(""); for (int m = 1; m< s.length(); m+) char a = s.charAt(m); tf.setText(tf.getText() + a); /如果当前字符串第一个字符不是符号,则添加一个符号在首字母处 else tf.setTex

22、t('-' + s); /实现%功能else if(obj=btMol)boolean isNumber = true;if(isNumber=true)tf.setText(String.valueOf(Double.parseDouble(tf.getText()/100);elsetf.setText("错误");/实现符号运算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("除数不能为零"); else tf.setText(String.valueOf(Double.parseDouble(foreNum)/Double.parseDouble(currNum);foreNum=tf.getText();tf.setText("");currOpr=e.getActionCommand();i = false;/实现等号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("除数不能为零"); elsetf.setText(String.valueOf(Double.parseDouble(foreNum)/Double.parseDouble(currNum);currOpr=e.getActionCommand();i = true;/实现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;/实现Ce功能else if (obj = btCe) tf.setText("0."); /实现C功能else if (obj = btCl) foreNum=currNum="" tf.setText("0.");/实现复制else if(obj=menuEditCopy)copy=tf.getText();/实现粘贴else if (obj = menuEditPaste) tf.setText(copy);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论