看了下API 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 还是dispose()好 以前没注意就一直用的setVisible(false); 虽然视觉效果是一样的 但没有释放资源 不太好
this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(-1); }
这个方法可以
那就setvisiable(false)吧
setVisible(false);只是隐藏
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 还是dispose()好 以前没注意就一直用的setVisible(false);
虽然视觉效果是一样的 但没有释放资源 不太好
public void windowClosing(WindowEvent e)
{
System.exit(-1);
}
正解!如果说使用了setVisible(),那么你就仅仅是把界面给隐藏了,但是依旧使用你的CPU
当你使用的是dispose()时,它会释放内存。两者的差距还是很大的
System.exit(-1);/**非正常退出*/
setVisible()在frame当中也有出现过
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class T { public static void main(String[] args) {
final Frame frame1 = new Frame("1");
final Frame frame2 = new Frame("2");
Frame frame3 = new Frame("3");
frame1.setBounds(0, 0, 200, 200);
frame2.setBounds(200, 0, 200, 200);
frame3.setBounds(400, 0, 200, 200);
frame1.setVisible(true);
frame2.setVisible(true);
frame3.setVisible(true);
frame1.addWindowListener(new WindowAdapter() {
@Override
// 关闭当前窗口。
public void windowClosing(WindowEvent e) {
frame1.dispose();
}
});
frame2.addWindowListener(new WindowAdapter() {
@Override
// 隐藏当前窗口。
public void windowClosing(WindowEvent e) {
frame2.setVisible(false);
}
});
frame3.addWindowListener(new WindowAdapter() {
@Override
// 系统退出。
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}}