界面如下(基于eclipse):
程序结构:
实现代码:
package calculatorimport java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Stack;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class Calculator {JFrame frame = new JFrame("计算器");Stack<String> OPTR = new Stack<String>();Stack<Double> OPND = new Stack<Double>();JPanel pNorth = new JPanel();JTextField inputTextField = new JTextField();JButton clearButton = new JButton("C");JPanel pCenter = new JPanel(new GridLayout(4, 4, 4, 4));String opndS;double opndD;String operator = null;String text = null;double result;int index = -1;public String Precede(String optr1, String optr2) {switch (optr1) {case "+":case "-":if (optr2.equals("*") || optr2.equals("/"))return "<";elsereturn ">";case "*":case "/":return ">";case "=":if (optr2.equals("="))return "=";elsereturn "<";default:return "";}}public double calculate(double a, String optr, double b) {switch (optr) {case "+":return a + b;case "-":return a - b;case "*":return a * b;case "/":return a / b;default:return 0;}}public void init() {OPTR.push("=");frame.setSize(252, 333);frame.setLocationRelativeTo(null);frame.setResizable(false);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);inputTextField.setPreferredSize(new Dimension(172, 53));inputTextField.setEditable(false);inputTextField.setFont(new Font("粗体", Font.BOLD, 15));inputTextField.setHorizontalAlignment(JTextField.RIGHT);clearButton.setForeground(Color.red);clearButton.setPreferredSize(new Dimension(56, 53));clearButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {inputTextField.setText("");}});pNorth.add(inputTextField);pNorth.add(clearButton);frame.add(pNorth, BorderLayout.NORTH);String btName = "123+456-789*0.=/";String regex = "[\\+\\-*/.=]";for (int i = 0; i < 16; i++) {String name = btName.substring(i, i + 1);JButton bt = new JButton();bt.setText(name);if (name.matches(regex)) {bt.setForeground(Color.blue);bt.setFont(new Font("粗体", Font.BOLD, 14));} else {bt.setFont(new Font("粗体", Font.ITALIC, 14));}bt.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String click = e.getActionCommand();if (click.matches("[\\+\\-*/=]")) {String input = inputTextField.getText();opndS = input.substring(index + 1);opndD = Double.parseDouble(opndS);OPND.push(opndD);switch (Precede(OPTR.peek(), click)) {case ">":while (Precede(OPTR.peek(), click).equals(">")) {double b = OPND.pop();double a = OPND.pop();String optr = OPTR.pop();double temp = calculate(a, optr, b);OPND.push(temp);}if (click.equals("="))result = OPND.pop();elseOPTR.push(click);break;case "<":OPTR.push(click);break;}if (click.equals("=")) {index = -1;inputTextField.setText(result + "");text = inputTextField.getText();} else {index = input.length();inputTextField.setText(inputTextField.getText() + e.getActionCommand());text = null;}} else if (text != null && !click.matches("[\\+\\-*/]")) {inputTextField.setText("");inputTextField.setText(inputTextField.getText() + e.getActionCommand());text = null;} else {inputTextField.setText(inputTextField.getText() + e.getActionCommand());}}});pCenter.add(bt);}frame.add(pCenter);}public static void main(String[] args) {new Calculator().init();}}