本帖最后由 通行天下 于 2013-4-5 14:39 编辑
import java.awt.*;
import java.awt.event.*;
public class TestMath {
public static void main(String[] args) {
new TFFrame1().launchFrame();
}
}
class TFFrame1 extends Frame {
TextField num1,num2,num3;//必须定义成成员变量
public void launchFrame() {
num1 = new TextField(8);
num2 = new TextField(8);
num3 = new TextField(10);
Label l = new Label("+");
Button b = new Button("=");
b.addActionListener(new MyMonitor(tf)); 这里用变量tf咋不对啊?该用什么啊?
setLayout(new FlowLayout());
add(num1);add(l);add(num2);add(b);add(num3);
pack();
setVisible(true);
}
}
class MyMonitor implements ActionListener {
TFFrame1 tf = null;
MyMonitor(TFFrame1 tf) {
this.tf = tf;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
tf.num3.setText(""+(n1+n2));
}
} |
|