日期:2014-05-20  浏览次数:20794 次

新手求指教啊!
import java.awt.*;
import java.awt.event.*;

public class TFMath {
public static void main(String[] args) {
new TFFrame().launchFrame();
}
}

class TFFrame extends Frame {
public void launchFrame() {
TextField t1 = new TextField(10);
TextField t2 = new TextField(10);
TextField t3 = new TextField(15);
Label la = new Label("+");
Button b = new Button("=");
b.addActionListener(new TFActionListener(this));
setLayout(new FlowLayout());
add(t1);
add(la);
add(t2);
add(b);
add(t3);
pack();
setVisible(true);

}
}

class TFActionListener implements ActionListener {

TFFrame tf = null;
public TFActionListener(TFFrame tf) {
this.tf = tf;
}

public void actionPerformed(ActionEvent e) {
int t11 = Integer.parseInt(tf.t1.getText());
int t22 = Integer.parseInt(tf.t2.getText());
tf.t3.setText(" "+ (t11+t22));
}
}

刚学这个持有对方引用,tf.t1.getText()总说找不到符号

------解决方案--------------------
你的t1定义的是个局部变量,肯定找不到啊。。。。