1. 程式人生 > 其它 >輸入框事件監聽TestFiled

輸入框事件監聽TestFiled

輸入框事件監聽TestFiled

package com.zishi.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestText01 {
public static void main(String[] args) {
//正常情況main只管啟動
new MyFrame();
}
}

class MyFrame extends Frame{
public MyFrame() {
TextField textField = new TextField();
add(textField); //已經繼承Frame,不需要再frame.add()

//監聽文字框輸入的文字
ActionListener myActionListenter3 = new MyActionListenter3();
textField.addActionListener(myActionListenter3);

//設定替換編碼
textField.setEchoChar('*');


pack();
setVisible(true);
}
}

class MyActionListenter3 implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
//e.getSource(); //獲得一些資源
TextField textField=(TextField) e.getSource(); //獲得一些資源,返回一個物件
System.out.println(textField.getText()); //獲得文字框中的內容
textField.setText(""); //設定為空 區別null ""
}
}