窗口监听事件
package com.zhang.Study.窗口监听事件;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Application {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.RED);
setBounds(100,100,200,200);
setVisible(true);
addWindowListener(new WindowFrameListener());
}
class WindowFrameListener extends WindowAdapter {
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
//获取窗口对象
WindowFrame source = (WindowFrame)(e.getSource());
source.setTitle("被激活了");
System.out.println("is Acrivted");
}
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
//这里把窗口显示为不可见,但是并没有关闭窗口
setVisible(false);//内部类可以调用外部类的属性和方法
System.exit(0);//真正关闭窗口操作
}
}
}