public class Calculate{
public double cal(double d1, double d2, String op){
double rel=0;
if(op.equals("+"))
rel=d1+d2;
else if(op.equals("-"))
rel=d1-d2;
else if(op.equals("*"))
rel=d1*d2;
else
if(d2==0)
rel=0;
else
rel=d1/d2;
return rel;
}
}
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class CalFrame extends JFrame implements ActionListener {
public CalFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
lbl_l1.setText("第一操作数");
lbl_l1.setBounds(new Rectangle(78, 60, 105, 31));
txt_d2.setBounds(new Rectangle(187, 143, 151, 30));
txt_d1.setBounds(new Rectangle(185, 59, 152, 28));
btn_cal.setBounds(new Rectangle(154, 248, 129, 34));
btn_cal.setText("计算");
btn_cal.addActionListener(this);
txt_result.setEnabled(false);
txt_result.setBounds(new Rectangle(189, 196, 150, 30));
lbl_l3.setText("计算结果");
lbl_l3.setBounds(new Rectangle(81, 196, 74, 26));
rdb_r1.setText("+");
rdb_r1.setBounds(new Rectangle(113, 111, 44, 23));
rdb_r2.setText("-");
rdb_r2.setBounds(new Rectangle(176, 112, 48, 23));
rdb_r3.setText("*");
rdb_r3.setBounds(new Rectangle(235, 112, 43, 23));
rdb_r4.setText("/");
rdb_r4.setBounds(new Rectangle(295, 110, 38, 24));
this.getContentPane().add(lbl_l1);
this.getContentPane().add(lbl_l3);
this.getContentPane().add(txt_result);
this.getContentPane().add(lbl_l2);
this.getContentPane().add(txt_d2);
this.getContentPane().add(txt_d1);
this.getContentPane().add(rdb_r3);
this.getContentPane().add(rdb_r2);
this.getContentPane().add(rdb_r1);
this.getContentPane().add(rdb_r4);
this.getContentPane().add(btn_cal);
lbl_l2.setText("第二操作数");
lbl_l2.setBounds(new Rectangle(80, 146, 87, 32));
rdb_r1.setSelected(true);
buttonGroup1.add(rdb_r1);
buttonGroup1.add(rdb_r2);
buttonGroup1.add(rdb_r3);
buttonGroup1.add(rdb_r4);
this.setTitle("简单计算器");
this.setSize(450, 350);
this.setVisible(true);
}
public static void main(String[] args) {
CalFrame calframe = new CalFrame();
}
JLabel lbl_l1 = new JLabel();
JLabel lbl_l2 = new JLabel();
JTextField txt_d1 = new JTextField();
JTextField txt_d2 = new JTextField();
JLabel lbl_l3 = new JLabel();
JTextField txt_result = new JTextField();
JButton btn_cal = new JButton();
JRadioButton rdb_r1 = new JRadioButton();
JRadioButton rdb_r2 = new JRadioButton();
JRadioButton rdb_r3 = new JRadioButton();
JRadioButton rdb_r4 = new JRadioButton();
ButtonGroup buttonGroup1 = new ButtonGroup();
ButtonGroup buttonGroup2 = new ButtonGroup();
public void actionPerformed(ActionEvent e) {
String d1 = txt_d1.getText();
String d2 = txt_d2.getText();
String op = "";
if (rdb_r1.isSelected()) {
op = rdb_r1.getActionCommand();
} else if (rdb_r2.isSelected()) {
op = rdb_r2.getActionCommand();
} else if (rdb_r3.isSelected()) {
op = rdb_r3.getActionCommand();
} else {
op = rdb_r4.getActionCommand();
}
CheckData cd = new CheckData();
boolean flag;
flag = cd.check(d1);
if (!flag) {
txt_d1.setText("");
txt_d1.requestFocus();
return;
}
flag = cd.check(d2);
if (!flag) {
txt_d2.setText("");
txt_d2.requestFocus();
return;
}
if(d2.equals("0") && op.equals("/")){
JOptionPane.showMessageDialog(null, "除数不能为0,请输入合法数字",
"提示", JOptionPane.ERROR_MESSAGE);
return;
}
double data1=Double.parseDouble(d1);
double data2=Double.parseDouble(d2);
Calculate calculate=new Calculate();
double result=calculate.cal(data1, data2, op);
txt_result.setText(String.valueOf(result));
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/450359a5ce15fc23693a62e668762bd9.png#pic_center)