窗口监听事件
package com.zishi.lesson03;
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,200,200,200);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
//关闭窗口
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
}
public void windowIconified(WindowEvent e) {
super.windowIconified(e);
}
public void windowDeiconified(WindowEvent e) {
super.windowDeiconified(e);
}
//激活窗口
public void windowActivated(WindowEvent e) {
WindowFrame source= (WindowFrame) e.getSource();
source.setTitle("激活了");
System.out.println("windowActivated");
}
public void windowDeactivated(WindowEvent e) {
super.windowDeactivated(e);
}
public void windowStateChanged(WindowEvent e) {
super.windowStateChanged(e);
}
public void windowGainedFocus(WindowEvent e) {
super.windowGainedFocus(e);
}
public void windowLostFocus(WindowEvent e) {
super.windowLostFocus(e);
}
}
);
}
// class MyWindowListener extends WindowAdapter{
// @Override
// public void windowClosing(WindowEvent e) {
// setVisible(false);//隐藏窗口,通过按钮隐藏当前窗口
// System.exit(0);
// }
// }
}
C:\Users\onefat\AppData\Roaming\Typora\typora-user-images