import java.awt.Frame; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TFActionEvent { public static void main(String[] args) { new TFFrame(); } } class TFFrame extends Frame { TFFrame() { TextField tf = new TextField(); this.add(tf); tf.addActionListener(new TFActionListener()); tf.setEchoChar('*'); // 设置回显字符 this.pack(); this.setVisible(true); } } class TFActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { TextField tf = (TextField) e.getSource(); // 这里拿到的TextField对象和上面构造的是一个对象 System.out.println(tf.getText()); tf.setText(""); } }