无聊写了个,修复了下Bug:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Calculate extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton plus, reduce, multiply, divice, reset;
private JTextField one, two, result;
private boolean device_falg = false;
private final int width = 400, height = 300;
public Calculate() {
super("修改密码");
this.setLayout(null);
this.setSize(width, height);
init();
Layout();
}
public void init() {
plus = new JButton("加 ");
reduce = new JButton("减 ");
multiply = new JButton("乘 ");
divice = new JButton("除 ");
reset = new JButton("清空");
one = new JTextField();
two = new JTextField();
result = new JTextField();
}
public void Layout() {
this.add(new JLabel("第一个数")).setBounds(20, 10, 60, 80);
this.add(one).setBounds(100, 38, 100, 25);
this.add(new JLabel("第二个数")).setBounds(20, 40, 60, 80);
this.add(two).setBounds(100, 70, 100, 25);
this.add(new JLabel("结果")).setBounds(20, 85, 60, 80);
this.add(result).setBounds(100, 110, 100, 25);
this.add(plus).setBounds(70, 170, 80, 25);
this.add(reduce).setBounds(200, 170, 80, 25);
this.add(multiply).setBounds(70, 200, 80, 25);
this.add(divice).setBounds(200, 200, 80, 25);
this.add(reset).setBounds(300, 220, 80, 25);
plus.addActionListener(this);
reduce.addActionListener(this);
multiply.addActionListener(this);
divice.addActionListener(this);
reset.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public boolean Format() {
boolean FLAG = false;
boolean flag = false;
String one = this.one.getText().toString().trim();
String two = this.two.getText().toString().trim();
if (one == null || one.equals("") || two == null || two.equals("")) {
JOptionPane.showMessageDialog(getParent(), "请输入完整信息!");
FLAG = false;
flag = true;
}
boolean boll_1 = one.matches("[\\d]{1,100}");
boolean boll_2 = two.matches("[\\d]{1,100}");
boolean boll_3 = one.matches("[\\d]{1,100}+[.]+[\\d]{1,100}");
boolean boll_4 = two.matches("[\\d]{1,100}+[.]+[\\d]{1,100}");
if (flag) {
return false;
}
if ((boll_1 && boll_2) || (boll_3 && boll_4) || (boll_1 && boll_4)
|| (boll_3 && boll_2)) {
FLAG = true;
} else {
JOptionPane.showMessageDialog(getParent(), "请输入数字!");
FLAG = false;
}
if (FLAG && device_falg) {
if (Double.parseDouble(two) == 0) {
JOptionPane.showMessageDialog(getParent(), "被除数不能为0!");
FLAG = false;
device_falg=false;
}
}
return FLAG;
}
public double Plus(double one, double two) {
return one + two;
}
public double Multiply(double one, double two) {
return one * two;
}
public double Divice(double one, double two) {
return one / two;
}
public double Reduce(double one, double two) {
return one - two;
}
public void Clear() {
one.setText("");
two.setText("");
result.setText("");
}
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o == reset) {
Clear();
return;
}
if (o == divice) {
device_falg = true;
}
if (!Format()) {
return;
}
double one = Double.parseDouble(this.one.getText());
double two = Double.parseDouble(this.two.getText());
double result = 0;
if (o == plus) {
result = Plus(one, two);
} else if (o == reduce) {
result = Reduce(one, two);
} else if (o == multiply) {
result = Multiply(one, two);
} else if (o == divice) {
result = Divice(one, two);
}
this.result.setText("" + result);
}
public static void main(String[] args) {
new Calculate();
}
}