2.2.9 窗口监听
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.blue);
setBounds(100,100,200,200);
setVisible(true);
//addWindowListener(new MyWindowListener());
//匿名内部类 更推荐
this.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
super.windowOpened(e);
}
@Override//关闭窗口
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
}
@Override
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
}
@Override //激活窗口
public void windowActivated(WindowEvent e) {
super.windowActivated(e);
}
});
}
// class MyWindowListener extends WindowAdapter{
// @Override
// public void windowClosing(WindowEvent e) {
// setVisible(false);//隐藏窗口
// System.exit(0);//正常退出
// }
// }
}