GUI编程(3)输入框的监听
package com.Jiu;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Notto4 {
public static void main(String[] args) {
MyFrame1 myFrame1=new MyFrame1(200,200,400,400);
}
}
class MyFrame1 extends Frame{
public MyFrame1(int x,int y,int w,int h){
setVisible(true);
setSize(w,h);
setLocation(x,y);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
TextField textField = new TextField();
add(textField);
textField.addActionListener(new MyActionListener1());
textField.setEchoChar('*');//设置替换编码
}
}
class MyActionListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field =(TextField) e.getSource();//获得输入框资源赋值给field,通过field来实现输入框的操作
System.out.println(field.getText());//输出输入框中的文本
field.setText("");
}
}