日期:2014-05-20 浏览次数:20862 次
import java.awt.Container;
import java.awt.FlowLayout;
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.JTextField;
public class Jisuan extends JFrame {
JTextField a1; //把这些当做成员变量
JTextField a2;
JButton b;
JTextField a3;
public Jisuan() {
super("SUM");
Container c = getContentPane();
c.setLayout(new FlowLayout());
a1 = new JTextField(" ", 4);
JLabel a0 = new JLabel("+");
a2 = new JTextField(" ", 4);
c.add(a1);
c.add(a0);
c.add(a2);
b = new JButton("=");
c.add(b);
a3 = new JTextField(" ", 4);
c.add(a3);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//这里面就不要定义哪些文本框了,
String s1 = a1.getText();
String s2 = a2.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double d3 = d1 + d2;
String s3 = Double.toString(d3);
a3.setText(s3);
}
});
}
public static void main(String args[]) {
Jisuan app = new Jisuan(); //注意这个地方错了
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(320, 120);
app.setVisible(true);
}
}