class loginWindow extends JFrame implements ActionListener
{
JTextField titleText;
JPasswordField passwordText;
JLabel title;
String eString;
loginWindow()
{
setSize(200, 150);
title = new JLabel("姓名");
titleText = new JTextField(10);
passwordText = new JPasswordField(10);
passwordText.setEchoChar('*');
titleText.addActionListener(this);
passwordText.addActionListener(this);
setLayout(new FlowLayout());
add(title);
add(titleText);
add(passwordText);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
eString=titleText.getText();
System.out.print(eString);}
public void actionPerformed(ActionEvent e)
{ // eString=titleText.getText();
if (e.getSource() == titleText)
System.out.println("用户名:" + eString);
}
为什么把 eString=titleText.getText()放在构造方法中,不能输出eString的值,而把 eString=titleText.getText()放在actionPerform中可以输出eString