主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码
1.Index.java
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;public class Index extends JFrame implements ActionListener {private int choice = 0;private double a = 0;private double b = 0;private double c = 0;private boolean next = true;public JButton one = new JButton("1");public JButton two = new JButton("2");public JButton three = new JButton("3");public JButton four = new JButton("4");public JButton five = new JButton("5");public JButton six = new JButton("6");public JButton seven = new JButton("7");public JButton eight = new JButton("8");public JButton nine = new JButton("9");public JButton zero = new JButton("0");public JButton point = new JButton(".");public JButton add = new JButton("+");public JButton subtract = new JButton("-");public JButton mult = new JButton("*");public JButton divi = new JButton("/");public JButton equal = new JButton("=");public JButton clean = new JButton("C");public JTextField message = new JTextField();public JTextField result = new JTextField();public JFrame jf = new JFrame("计算器");public Container con = jf.getContentPane();public Font font = new Font("楷体", 0, 15);public Font font1 = new Font("楷体", 0, 20);Index() {jf.setBounds(90, 90, 250, 220);jf.setResizable(false);jf.setVisible(true);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setResizable(false);jf.setLocationRelativeTo(null);message.setBounds(5, 5, 185, 15);message.setBorder(null);message.setBackground(Color.white);message.setFont(font);message.setEditable(false);result.setBounds(5, 20, 185, 25);result.setBorder(null);result.setBackground(Color.white);result.setFont(font1);result.setEditable(false);//设置为不可写,即只读clean.setBounds(195, 5, 45, 40);clean.setFont(font);clean.addActionListener(this);one.setBounds(5,50,50,30);one.setFont(font);one.addActionListener(this);two.setBounds(60,50,50,30);two.setFont(font);two.addActionListener(this);three.setBounds(120,50,50,30);three.setFont(font);three.addActionListener(this);add.setBounds(180, 50, 60, 30);add.setFont(font);add.addActionListener(this);four.setBounds(5,85,50,30);four.setFont(font);four.addActionListener(this);five.setBounds(60,85,50,30);five.setFont(font);five.addActionListener(this);six.setBounds(120,85,50,30);six.setFont(font);six.addActionListener(this);subtract.setBounds(180, 85, 60, 30);subtract.setFont(font);subtract.addActionListener(this);seven.setBounds(5,120,50,30);seven.setFont(font);seven.addActionListener(this);eight.setBounds(60,120,50,30);eight.setFont(font);eight.addActionListener(this);nine.setBounds(120,120,50,30);nine.setFont(font);nine.addActionListener(this);mult.setBounds(180, 120, 60, 30);mult.setFont(font);mult.addActionListener(this);point.setBounds(5,155,50,30);point.setFont(font);point.addActionListener(this);zero.setBounds(60,155,50,30);zero.setFont(font);zero.addActionListener(this);equal.setBounds(120,155,50,30);equal.setFont(font);equal.addActionListener(this);divi.setBounds(180, 155, 60, 30);divi.setFont(font);divi.addActionListener(this);con.setLayout(null);//不用默认的布局方式(布局管理器)con.setVisible(true);con.add(message);con.add(result);con.add(clean);con.add(one);con.add(two);con.add(three);con.add(add);con.add(four);con.add(five);con.add(six);con.add(subtract);con.add(seven);con.add(eight);con.add(nine);con.add(mult);con.add(point);con.add(zero);con.add(equal);con.add(divi);}private static void hint(String str) {JOptionPane.showMessageDialog(null, str, "提示", JOptionPane.INFORMATION_MESSAGE);}private void clean(String str) {this.message.setText(str);this.result.setText("");}public void actionPerformed(ActionEvent e) {if(e.getSource() == this.one) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"1");}else if(e.getSource() == this.two) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"2");}else if(e.getSource() == this.three) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"3");}else if(e.getSource() == this.four) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"4");}else if(e.getSource() == this.five) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"5");}else if(e.getSource() == this.six) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"6");}else if(e.getSource() == this.seven) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"7");}else if(e.getSource() == this.eight) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"8");}else if(e.getSource() == this.nine) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"9");}else if(e.getSource() == this.zero) {if(next) {clean("");this.next = false;}this.result.setText(result.getText()+"0");}else if(e.getSource() == this.point) {this.result.setText(result.getText()+".");this.next = false;//}else if(e.getSource() == this.add) {//加String number = this.result.getText();String str = this.message.getText() + this.result.getText() + "+";if(number.equals("")){hint("请输入数据!");return;}a = Double.parseDouble(number);this.choice = 1;this.message.setText(str);this.result.setText("");this.next = false;}else if(e.getSource() == this.subtract) {//减String number = this.result.getText();String str = this.message.getText() + this.result.getText() + "-";if(number.equals("")){this.result.setText("-");}else{a = Double.parseDouble(number);this.choice = 2;this.message.setText(str);this.result.setText("");}this.next = false;}else if(e.getSource() == this.mult) {//乘String number = this.result.getText();String str = this.message.getText() + this.result.getText() + "*"+ "";if(number.equals("")){hint("请输入数据!");return;}a = Double.parseDouble(number);this.choice = 3;this.message.setText(str);this.result.setText("");this.next = false;}else if(e.getSource() == this.divi) {//除String number = this.result.getText();String str = this.message.getText() + this.result.getText() + "/";if(number.equals("")){hint("请输入数据!");return;}a = Double.parseDouble(number);this.choice = 4;this.message.setText(str);this.result.setText("");this.next = false;}else if(e.getSource() == this.equal){String number = this.result.getText();if(number.equals("")){hint("请输入数据!");return;}b = Double.parseDouble(number);if(b == 0 && choice == 4) {hint("除数不能为0!");clean("");return;}String str = this.message.getText() + this.result.getText() + "=";this.message.setText(str);c = Counter.compute(a,b,choice);String re = new String();re += c;int L = re.length();if(re.charAt(L-2) == '.' && re.charAt(L-1) == '0') re = re.substring(0, L-2);this.result.setText(re);this.next = true;}else if(e.getSource() == this.clean) {clean("");}}}
2.Counter.java
public class Counter {public static void main(String[] args) {new Index();}public static double compute(double a, double b, int choice) {double c = 0;switch(choice) {case 1: c = a + b;break;case 2: c = a - b;break;case 3: c = a * b;break;case 4: c = a / b;break;}
//System.out.println(c);return c;}}