资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
本程序实现了,java计算器,+ - * / 等简单的四则运算,且可以在有结果的基础上继续进行简单运算。实现了: + - * / 的自由切换的自由运用(可以在第一个数亦可在第二个数子的取反) 解决了其他计算器0.8+.8没有解决的问题(小数点问题) 解决了其他计算器可以多次输入0的问题 解决了整型浮点型的问题如 4+4=8而不会显示8.0 本计算器能运行整型和浮点型运算,且结果显示正常import java.awt.Container;importjava.awt.GridLayout;importjava.awt.TextComponent;importjava.awt.TextField;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.WindowConstants;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(计算器);setLayout(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 windowContainer=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 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();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.addActionListener(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);num4.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.setBounds(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.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);windowContainer.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(WindowConstants.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.parseDouble(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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号