class MyWindowListener extends WindowAdapter{
private Frame f;
public MyWindowListener(Frame f){
this.f = f;
}
public void windowClosing(WindowEvent arg0) {
this.f.dispose();
System.out.println("Closing...");
//System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
System.out.println("Colsed");
}
关闭窗口的时候使用System.exit(0);并不能触发WindowClosed()方法,有人说使用dispose()方法可以触发,但是不明白的是,为什么调用disposed()之后再用,System.exit()退出程序却不能触发WindowClosed()方法了呢?
如果将WindowClosing()改成这样:
public void windowClosing(WindowEvent arg0) {
this.f.dispose();
System.out.println("Closing...");
//System.exit(0);
}
我们能够看到运行结果是这样的:
Closing...
Colsed
那么,这种情况下,WindowClosed()又是怎么被调用的呢?
private Frame f;
public MyWindowListener(Frame f){
this.f = f;
}
public void windowClosing(WindowEvent arg0) {
this.f.dispose();
System.out.println("Closing...");
//System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
System.out.println("Colsed");
}
关闭窗口的时候使用System.exit(0);并不能触发WindowClosed()方法,有人说使用dispose()方法可以触发,但是不明白的是,为什么调用disposed()之后再用,System.exit()退出程序却不能触发WindowClosed()方法了呢?
如果将WindowClosing()改成这样:
public void windowClosing(WindowEvent arg0) {
this.f.dispose();
System.out.println("Closing...");
//System.exit(0);
}
我们能够看到运行结果是这样的:
Closing...
Colsed
那么,这种情况下,WindowClosed()又是怎么被调用的呢?
本文探讨了Java中窗口监听机制的细节,特别是WindowAdapter的使用方法。通过具体代码示例,详细解释了windowClosing和windowClosed事件触发条件及两者之间的区别。
74





