import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MiniCalculator extends JFrame implements WindowListener { private JPanel jp; private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9; private JButton bPoi, bAdd, bDec, bMul, bDiv, bCal; private JTextField t; private String s, op; private Calculator cal = new Calculator(); // 是否点击了操作符 private boolean ifOp = false; public MiniCalculator(String title) { super(title); // 创建两个面板,一个用来放文本框,一个用来放16个按钮 jp = new JPanel(); // 文本框 t = new JTextField("", 25); // 将文本框设置为不可编辑,禁止用户直接对其内容进行编辑 t.setEditable(false); Font font=new Font("SansSerif",Font.BOLD+Font.ITALIC,36); t.setFont(font); t.setForeground(Color.PINK); jp.add(t); // 创建16个按钮 b0 = new JButton("0"); b1 = new JButton("1"); b2 = new JButton("2"); b3 = new JButton("3"); b4 = new JButton("4"); b5 = new JButton("5"); b6 = new JButton("6"); b7 = new JButton("7"); b8 = new JButton("8"); b9 = new JButton("9"); bPoi = new JButton("."); bAdd = new JButton("+"); bDec = new JButton("-"); bMul = new JButton("x"); bDiv = new JButton("/"); bCal = new JButton("="); // 为16个按钮添加监听者 b0.addActionListener(new NumberActionListener()); b1.addActionListener(new NumberActionListener()); b2.addActionListener(new NumberActionListener()); b3.addActionListener(new NumberActionListener()); b4.addActionListener(new NumberActionListener()); b5.addActionListener(new NumberActionListener()); b6.addActionListener(new NumberActionListener()); b7.addActionListener(new NumberActionListener()); b8.addActionListener(new NumberActionListener()); b9.addActionListener(new NumberActionListener()); bPoi.addActionListener(new NumberActionListener()); bAdd.addActionListener(new OperatorActionListener()); bDec.addActionListener(new OperatorActionListener()); bMul.addActionListener(new OperatorActionListener()); bDiv.addActionListener(new OperatorActionListener()); bCal.addActionListener(new OperatorActionListener()); // 将16个按钮加入面板2 jp.setLayout(new GridLayout(4,4)); jp.add(b7); jp.add(b8); jp.add(b9); jp.add(bAdd); jp.add(b4); jp.add(b5); jp.add(b6); jp.add(bDec); jp.add(b1); jp.add(b2); jp.add(b3); jp.add(bMul); jp.add(bPoi); jp.add(b0); jp.add(bCal); jp.add(bDiv); // 将2个面板加入到窗口 add(t,BorderLayout.NORTH); add(jp,BorderLayout.SOUTH); setSize(300,193); // 设置窗口大小固定 setResizable(false); addWindowListener(this); setVisible(true); } // 设置文本框的文本显示 public void setTextFieldText(String s) { // 如果已经点击过操作符 if (ifOp) { // 更改"是否点击过操作符"的标记并清空文本框,准备接受下一个数 ifOp = false; t.setText(""); } // 将原有字符加上本次输入的字符就是本次点击后应显示的字符 t.setText(t.getText() + s); } // 点击数字和小数点按钮时监听者 class NumberActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton tempB = (JButton) e.getSource(); s = tempB.getLabel(); setTextFieldText(s); } } // 点击操作符按钮时监听者 class OperatorActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton tempB = (JButton) e.getSource(); op = tempB.getLabel(); String number = t.getText(); if (!number.equals("")) { if (op.equals("+")){ t.setText(cal.opAdd(number)); ifOp = true; }else if (op.equals("-")) { t.setText(cal.opSubtract(number)); ifOp = true; }else if (op.equals("x")) { t.setText(cal.opMultiply(number)); ifOp = true; }else if (op.equals("/")) { t.setText(cal.opDivide(number)); ifOp = true; }else if (op.equals("=")) { t.setText(cal.opEquals(number)); ifOp = true; }else { t.setText("请先输入数字再点击操作符"); ifOp = true; } } } } public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit(0); } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} / public static void main(String args[]) { MiniCalculator calculator = new MiniCalculator("计算器学习"); } /** * 算法的思路是:例如计算A+B,当输入了A后第一次点击"+",先将A存放在result里, * 并将操作符标记为"add",当用户输入了B后,在点击了"="后,进行计算即 * result+=B,返回result即为结果。 * */ public class Calculator { //计算前的值 private String result = "0"; /* * op表示点击了操作符 * add表示进行加法 * sub表示进行减法 * mul表示进行乘法 * div表示进行除法 */ private int op = 0, add = 1, sub = 2, mul = 3, div = 4; //字符串转double private double stringToDouble(String x) { double y = Double.parseDouble(x); return y; } //加 public String opAdd(String x) { operate(x); op = add; return result; } //减 public String opSubtract(String x) { operate(x); op = sub; return result; } //乘 public String opMultiply(String x) { operate(x); op = mul; return result; } //除 public String opDivide(String x) { operate(x); op = div; return result; } //等号 public String opEquals(String x) { operate(x); op = 0; return result; } //根据实际情况实行真正的操作符计算 private void operate(String x) { double number2 = stringToDouble(x); double number1 = stringToDouble(result); switch (op) { //输入了一个数后第一次点击操作符 case 0: result = x; break; //计算加法 case 1: result = String.valueOf(number1 + number2); break; //计算减法 case 2: result = String.valueOf(number1 - number2); break; //计算乘法 case 3: result = String.valueOf(number1 * number2); break; //计算除法 case 4: if (number2 != 0) { result = String.valueOf(number1 / number2); } else { result = "除数不能为0"; } break; } } } }