---- Test_TextField01.java
import java.awt.*;
import java.awt.event.*;
public class Test_TextField01 implements ActionListener {
static TextField tf1 = new TextField();//为什么static能获取到值
TextField tf2 = new TextField();//为什么不能获取值
// static TextField tf1=new TextField();
// static TextField tf2=new TextField();
static String s1 = tf1.getText();
String s2 = tf2.getText();
Label l1 = new Label("输入01:");
Label l2 = new Label("输入02:");
Button b1 = new Button("OK");
Button b2 = new Button("cancel");
Frame f = new Frame("Test");
public void TestFrame() {
f.setLayout(new GridLayout(3, 2));
f.add(l1);
f.add(tf1);
f.add(l2);
f.add(tf2);
f.add(b1);
f.add(b2);
f.setSize(200, 100);
f.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
(new Test_TextField02()).action_Method();
} else if (e.getSource() == b2) {
System.exit(0);
}
}
}
============================================================
---- Test_TextField02.java
public class Test_TextField02 {
Test_TextField01 t1 = new Test_TextField01();
String s1 = t1.s1;
String s2 = t1.s2;
public void action_Method() {
System.out.println("Text01:" + s1);
System.out.println("Text02:" + s2);
}
}
-------------------------------------------------------------
在另一个单独类中获取textField的值,为什么static能被获取,而动态的不能??!!