过程
1 定义事件监听器的类 实现implements ActionListener , 实现方法 actionPerformed(ActionEvent e)
ActionEvent e 这个参数接收的是用户的行为,比如鼠标点击,关闭某个文件等等,用户行为触发actionPerformed(ActionEvent e) 执行
所以监听过程不一定要执行,一定要有触发行为
package awt2yue25;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EventQS {
private Frame f = new Frame("测试窗口");
private Button ok = new Button("ok");
private TextField tf= new TextField(30);
public void init()
{
//吧事件监听器 new OKListener() 注册到ok按钮上面
ok.addActionListener(new OKListener());
f.add(tf);
f.add(ok,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
//定义事件监听器类,属于内部类 实现接口,重写方法
class OKListener implements ActionListener
{
//ActionEvent e这个参数接收事件,比如用户点击事件,才能激活事件监听和处理过程
public void actionPerformed(ActionEvent e) {
System.out.println("用户单击了ok按钮");
tf.setText("hello world");
}
}
public static void main(String[] args)
{
new EventQS().init();
}
}