Java学习-GUI编程-输入框事件监听
public class TestText01 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField tf = new TextField();
add(tf);
MyActionListener2 mal = new MyActionListener2();
tf.addActionListener(mal);
pack();
setVisible(true);
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField tf = (TextField) e.getSource();
System.out.println(tf.getText());
}
}
输入字符显示为‘*’,保证安全性。
public MyFrame(){
TextField tf = new TextField();
add(tf);
MyActionListener2 mal = new MyActionListener2();
tf.addActionListener(mal);
pack();
setVisible(true);
tf.setEchoChar('*');
}
输出后文本清空
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField tf = (TextField) e.getSource();
System.out.println(tf.getText());
tf.setText("");
}
}