import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WindowListenerTest {
public static void main(String[] args) {
new MyFrame6();
}
}
class MyFrame6 extends Frame {
public MyFrame6(){
setBackground(Color.PINK);
setBounds(100,100,200,200);
setVisible(true);
//以内部类的形式创建窗口监听器
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
}
运行结果为:
打开一个粉色窗口,点击右上角可以关闭窗口,程序终止运行