GUI-文本框監聽事件
//輸入框監聽事件
public class TextText01 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
//TextField是一個檔案框組件
TextField textField = new TextField();
add(textField);
//監聽這個文本框輸入的文字
MyActionListener02 myActionListener02 = new MyActionListener02();
//按下enter 就會觸發這個輸入框事件
textField.addActionListener(myActionListener02);
//設定替換編碼
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener02 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//型別轉換
TextField field =(TextField) e.getSource();//獲取一些資源,回傳一個物件
System.out.println(field.getText());//獲得輸入框的文本
field.setText("");//按下空格后清零,這里不能用null 因為null是一個物件,""是一個字串
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/279942.html
標籤:其他
