8.窗口监听
作者:互联网
1.窗口监听
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Dome3 {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setBounds(500, 500, 200, 300);
setVisible(true);
//addWindowListener(new MyWindowListener());//内部类调用
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
//窗口关闭
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口被关闭");
}
//窗口激活
@Override
public void windowActivated(WindowEvent e) {
System.out.println("窗口被激活");
}
});
}
//窗口监听内部类
class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
这里没有写关闭的操作,所以点击关闭后,窗口不会被关闭
监听窗口,这里写了两种方式,一种是调用内部类,一种是匿名内部类,窗口监听事件经常使用的有2种,关闭和激活,后面有遇到的画在补充吧...........................
标签:窗口,void,public,关闭,WindowEvent,监听 来源: https://www.cnblogs.com/lyq888/p/16091186.html