问1: JWindow w=new JWindow();
w.setBounds(200, 500, 200, 200);
w.setVisible(true);
w.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.out.println("close");
}
});
w.dispose();结果:
close
close输出了2次为什么?问2:java.awt.Container如何释放资源
Container 没有dispose 方法,setEnabled(false)也是没用的,setVisible(false)只是隐藏罢了
w.setBounds(200, 500, 200, 200);
w.setVisible(true);
w.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.out.println("close");
}
});
w.dispose();结果:
close
close输出了2次为什么?问2:java.awt.Container如何释放资源
Container 没有dispose 方法,setEnabled(false)也是没用的,setVisible(false)只是隐藏罢了
dispose是释放占用的资源。
Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be ed as undisplayable.
The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions). Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. 释放的是native screen resources,但事后可重建,可能还是维护了一些状态信息的,但是dispose之后对window的修改不计在内(dispose之后还能修改啊)
我看到dispose源码,会响应WindowEvent.WINDOW_CLOSED
postWindowEvent(WindowEvent.WINDOW_CLOSED);
还是不是很明白为什么触发了2次windowClosed,也就是w作为顶层容器就没办法释放资源了?只能隐藏不用(setVisible(false)),让GC解决?
Container w=new JWindow();