java简易计算器源代码.doc

上传人:仙人指路1688 文档编号:2386956 上传时间:2023-02-17 格式:DOC 页数:7 大小:43KB
返回 下载 相关 举报
java简易计算器源代码.doc_第1页
第1页 / 共7页
java简易计算器源代码.doc_第2页
第2页 / 共7页
java简易计算器源代码.doc_第3页
第3页 / 共7页
java简易计算器源代码.doc_第4页
第4页 / 共7页
java简易计算器源代码.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《java简易计算器源代码.doc》由会员分享,可在线阅读,更多相关《java简易计算器源代码.doc(7页珍藏版)》请在三一办公上搜索。

1、package Swing;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;i

2、mport javax.swing.JTextField;public class Work1 public static void main(String args) new JsqView(340, 240);SuppressWarnings(serial)class JsqView extends JFrame JTextField jtf;JButton jb1, jb2, jb3;JButton jb;JPanel jp1, jp2, jp3, jp4;MyActionListener l;public JsqView(int weith, int heigh) Container

3、con = this.getContentPane();con.setLayout(new BorderLayout(5, 10);l = new MyActionListener(this);jtf = new JTextField(26);jp1 = new JPanel();jp1.add(jtf);jtf.setHorizontalAlignment(JTextField.RIGHT);jp3 = new JPanel();jp3.setLayout(new BorderLayout(5, 10);jb1 = new JButton(Backspace);jb2 = new JButt

4、on(CE);jb3 = new JButton(C);jb1.setForeground(Color.red);jb1.addActionListener(l);jb2.addActionListener(l);jb2.setForeground(Color.red);jb3.addActionListener(l);jb3.setForeground(Color.red);jp2 = new JPanel();jp2.setLayout(new GridLayout(1, 3, 5, 5);jp2.add(jb1);jp2.add(jb2);jp2.add(jb3);jp4 = new J

5、Panel();jp4.setLayout(new GridLayout(4, 5, 5, 5);jb = new JButton20;String str = 7, 8, 9, /, sqrt, 4, 5, 6, *, %,1, 2, 3, -, 1/x, 0, +/-, ., +, = ;for (int i = 0; i 20; i+) jbi = new JButton(stri);jbi.addActionListener(l);if (i = 3 | i = 8 | i = 13 | i = 18 | i = 19) jbi.setForeground(Color.red);jp4

6、.add(jbi);jp3.add(jp2, BorderLayout.NORTH);jp3.add(jp4);con.add(jp1, BorderLayout.NORTH);con.add(jp3);this.setTitle(计算器);this.setSize(weith, heigh);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);class MyActionListener implements ActionListener JsqView jv;char operator;Stri

7、ng temp;Count c = new Count();boolean b = false;public MyActionListener(JsqView jv) this.jv = jv;public void actionPerformed(ActionEvent e) String str = e.getActionCommand().trim();/System.out.println(str);if (str.matches(d.) /System.out.println(str);if(b)jv.jtf.setText();b = false;StringBuffer sbf

8、= new StringBuffer();String s = jv.jtf.getText().trim();sbf.append(s);if(.equals(s)&.equals(str)sbf.append(0+str);else if(s.matches(d+)sbf.append(str);else if(str.matches(d)sbf.append(str);jv.jtf.setText(sbf.toString(); else if (str.matches(+-*/%) b =false;operator = str.charAt(0);/System.out.printl

9、n(str);temp = jv.jtf.getText().trim();jv.jtf.setText(); else if (str.equals(=) String s = jv.jtf.getText().trim();/System.out.println(temp);/System.out.println(s);if(.equals(s)|s=null|temp=null)return;if(temp.matches(d+.d+)|(s.matches(d+.d+)double i = Double.parseDouble(temp);double j = Double.parse

10、Double(s);if(!b)temp=s;double sum =c.count(i, j, operator,jv);jv.jtf.setText(sum + );elselong i = Long.parseLong(temp);long j = Long.parseLong(s);if(!b)temp=s;long sum = c.count(i, j, operator,jv);jv.jtf.setText(sum + );b = true;else if(str.equals(1/x)String s = jv.jtf.getText().trim();double i = Do

11、uble.parseDouble(s);double sum = c.count(i,jv);jv.jtf.setText(sum + );b = true;else if(str.equals(CE)b= false;jv.jtf.setText();else if(str.equals(Backspace)b= false;jv.jtf.setText(temp);else if(str.equals(sqrt)b= true;String s = jv.jtf.getText().trim();double b = Math.sqrt(Double.parseDouble(s);jv.j

12、tf.setText(b+);else if(str.equals(+/-)String s = jv.jtf.getText().trim();if(s.startsWith(-)s= s.substring(1, s.length();elses = -+s;jv.jtf.setText(s);class Count public long count(long i,long j,char operator,JsqView jv)long num = 0;switch (operator) case +:num = i + j;break;case -:num = i - j;break;

13、case *:num = i * j;break;case /:if(j=0)try throw new MyException(); catch (MyException e) JOptionPane.showMessageDialog(jv, 被除数不能为零,错误,JOptionPane.ERROR_MESSAGE);return 0;num = i / j;break;case %:num = i % j;break;default:break;return num;public double count(double i,double j,char operator,JsqView j

14、v)double num = 0;switch (operator) case +:num = i + j;break;case -:num = i - j;break;case *:num = i * j;break;case /:if(j=0.0)try throw new MyException(); catch (MyException e) JOptionPane.showMessageDialog(jv, 被除数不能为零,错误,JOptionPane.ERROR_MESSAGE);return 0;num = i / j;break;default:break;return num

15、;public double count (double i,JsqView jv)double num;if(i=0.0)try throw new MyException(); catch (MyException e) JOptionPane.showMessageDialog(jv, 被除数不能为零,错误,JOptionPane.ERROR_MESSAGE);return 0;num = 1/i;return num;SuppressWarnings(serial)class MyException extends Exceptionpublic MyException()super(除数不能为零);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号