import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JavaCalum extends JFrame implements ActionListener{
private JButton jb1 = new JButton("=");
private JTextField jt1 = new JTextField(10);
private JTextField jt2 = new JTextField(10);
private JTextField jt3 = new JTextField(10);
private JLabel jl = new JLabel("+");
public JavaCalum() {
super.setTitle("简易计算器");
super.setBounds(200,200,500,500);
super.getContentPane().setLayout(new FlowLayout());
super.getContentPane().add(jt1);
super.getContentPane().add(jl);
super.getContentPane().add(jt2);
super.getContentPane().add(jb1);
super.getContentPane().add(jt3);
jb1.addActionListener(this);
super.setVisible(true);
}
public static void main(String args[]){
new JavaCalum();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb1){
double a = Double.parseDouble(jt1.getText());
double b = Double.parseDouble(jt2.getText());
double c = a+b;
String string= Double.toString(c);
jt3.setText(string);
}
}
}
//调试过了可以用,可以计算带小数点的加法。
温馨提示:答案为网友推荐,仅供参考