import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
//除零异常处理
class Divzero extends Exception {
Divzero(String msg) {
super(msg);
}
}
abstract class Operation { // 运算类
public abstract double operation(double x, double y);
}
class Add extends Operation { // 子类加法
public double operation(double x, double y) {
return x + y;
}
}
class Sub extends Operation { // 子类减法
public double operation(double x, double y) {
return x - y;
}
}
class Mul extends Operation { // 子类乘法
public double operation(double x, double y) {
return x * y;
}
}
class Div extends Operation { // 子类除法
public double operation(double x, double y) {
return x / y;
}
}
// 绑定运算符号对象
class Factory {
public static Operation factory(String operation) { //根据运算符创建相应的操作对象
switch (operation) {
case "+":
return new Add();
case "-":
return new Sub();
case "×":
return new Mul();
case "÷":
return new Div();
default: return null;
}
}
}
public class calculator {
// 创建按钮
private JPanel JPanel;
private JButton B8;
private JButton B9;
private JButton B7;
private JButton B5;
private JButton B6;
private JButton B4;
private JButton B2;
private JButton B3;
private JButton B1;
private JButton B0;
private JButton Bpoint;
private JButton Bsign;
private JButton Bc;
private JButton Bbc;
private JButton Bdiv;
private JButton Bsub;
private JButton Badd;
private J