java完全简单计算器

上传人:小** 文档编号:89347080 上传时间:2019-05-23 格式:DOC 页数:9 大小:38.50KB
返回 下载 相关 举报
java完全简单计算器_第1页
第1页 / 共9页
java完全简单计算器_第2页
第2页 / 共9页
java完全简单计算器_第3页
第3页 / 共9页
java完全简单计算器_第4页
第4页 / 共9页
java完全简单计算器_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《java完全简单计算器》由会员分享,可在线阅读,更多相关《java完全简单计算器(9页珍藏版)》请在金锄头文库上搜索。

1、本程序实现了,java计算器,+ - * / 等简单的四则运算,且可以在有结果的基础上继续进行简单运算。实现了: + - * / 的自由切换的自由运用(可以在第一个数亦可在第二个数子的取反) 解决了其他计算器0.8+.8没有解决的问题(小数点问题) 解决了其他计算器可以多次输入0的问题 解决了整型浮点型的问题如 4+4=8而不会显示8.0 本计算器能运行整型和浮点型运算,且结果显示正常import java.awt.Container;importjava.awt.GridLayout;importjava.awt.TextComponent;importjava.awt.TextField;

2、import java.awt.event.ActionEvent;import java.awt.event.ActionListener;importjava.awt.event.TextEvent;importjava.awt.event.TextListener;importjavax.swing.Icon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JTextField;import javax.swing.WindowCo

3、nstants;publicclassCLACextends JFrameJTextField numText,resultText;JButton num1,num2,num3,num4,num5,num6,num7,num8,num9,num0,spot,plus,nminus,Mul,divide,clear,result,c ,negative;doublex,y;StringBuffer str;intz;doublenum=0;ActionListener al=null;booleanfreshstart = true;public CLAC()setTitle(计算器);set

4、Layout(null);setBounds(0, 0, 370, 370);/TextField numText= new TextField(0);numText=new JTextField(0);resultText=new JTextField();numText.setHorizontalAlignment(JTextField.RIGHT);resultText.setHorizontalAlignment(JTextField.RIGHT);numText.setEditable(false);resultText.setEditable(false);Container wi

5、ndowContainer=getContentPane();num1=new JButton(1);num1.addActionListener(new Bt();num2=new JButton(2);num2.addActionListener(new Bt();num3=new JButton(3);num3.addActionListener(new Bt();num4=new JButton(4);num4.addActionListener(new Bt();num5=new JButton(5);num5.addActionListener(new Bt();num6=new

6、JButton(6);num6.addActionListener(new Bt();num7=new JButton(7);num7.addActionListener(new Bt();num8=new JButton(8);num8.addActionListener(new Bt();num9=new JButton(9);num9.addActionListener(new Bt();num0=new JButton(0);num0.addActionListener(new Bt();spot=new JButton(.);spot.addActionListener(new Bt

7、();plus=new JButton(+);plus.addActionListener(new Bt();nminus=new JButton(-);nminus.addActionListener(new Bt();Mul=new JButton(*);Mul.addActionListener(new Bt();divide=new JButton(/);divide.addActionListener(new Bt();clear=new JButton(CE);clear.addActionListener(new Bt();c=new JButton(C);c.addAction

8、Listener(new Bt();negative=new JButton();negative.addActionListener(new Bt();result=new JButton(=);result.addActionListener(new Bt();num1.setBounds(10, 100, 60, 30);num2.setBounds(80, 100, 60, 30);num3.setBounds(150, 100, 60, 30);plus.setBounds(220, 100, 60, 30);clear.setBounds(290, 100, 60, 30);num

9、4.setBounds(10, 140, 60, 30);num5.setBounds(80, 140, 60, 30);num6.setBounds(150,140, 60, 30);nminus.setBounds(220, 140, 60, 30);c.setBounds(290, 140, 60, 30);num7.setBounds(10, 180, 60, 30);num8.setBounds(80, 180, 60, 30);num9.setBounds(150, 180, 60, 30);Mul.setBounds(220, 180, 60, 30);result.setBou

10、nds(290, 180, 60, 70);num0.setBounds(10, 220, 130, 30);spot.setBounds(150, 220, 60, 30);divide.setBounds(220, 220, 60, 30);negative.setBounds(10, 260, 340, 30);numText.setBounds(10,50, 340, 35);resultText.setBounds(10, 10, 340, 35);windowContainer.add(num1);windowContainer.add(num2);windowContainer.

11、add(num3);windowContainer.add(num4);windowContainer.add(num5);windowContainer.add(num6);windowContainer.add(num7);windowContainer.add(num8);windowContainer.add(num9);windowContainer.add(num0);windowContainer.add(spot);windowContainer.add(plus);windowContainer.add(nminus);windowContainer.add(Mul);win

12、dowContainer.add(divide);/windowContainer.add(negative);windowContainer.add(result);windowContainer.add(clear);windowContainer.add(c);windowContainer.add(negative);windowContainer.add(numText);windowContainer.add(resultText);setVisible(true);setResizable(false);setDefaultCloseOperation(WindowConstan

13、ts.EXIT_ON_CLOSE);str=new StringBuffer();class Bt implements ActionListenerOverridepublicvoid actionPerformed(ActionEvent e) / TODO 自动生成的方法存根if(e.getSource()=plus)x=Double.parseDouble(numText.getText().trim();resultText.setText(x+);str.setLength(0);y=0d;z=0;elseif(e.getSource()=nminus)x=Double.parse

14、Double(numText.getText().trim();resultText.setText(x+-);str.setLength(0);y=0d;z=1;elseif(e.getSource()=Mul)x=Double.parseDouble(numText.getText().trim();resultText.setText(x+*);str.setLength(0);y=0d;z=2;elseif(e.getSource()=divide)x=Double.parseDouble(numText.getText().trim();resultText.setText(x+/);str.setLength(0);y=0;z=3;elseif(e.getSource()=result)str.setLength(0);switch (z) case 0:y= Double.parseDouble(numText.getText().trim();num= x+y;doubl(num);break;case 1:y= Double.parseDouble(numText.getText().trim();num= x-y;doubl(num);break;case 2:y

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号