Java JFrame窗口关闭方法总结
之前写窗口时每次都是直接copy关闭的方法,没有想过其它的,最近写的窗口比较多,就总结了一些窗口关闭的方法有哪些,方便以后自己查阅。
public void setDefaultCloseOperation(int operation):
默认关闭状态:JFrame.class中: private int defaultCloseOperation =HIDE_ON_CLOSE;
因此,默认情况下,关闭窗口,只隐藏界面,不释放占用的内存。
点击窗口右上角关闭,四种关闭方式:
1.this.setDefaultCloseOperation(0);// DO_NOTHING_ON_CLOSE,不执行任何操作。
2.this.setDefaultCloseOperation(1);//HIDE_ON_CLOSE,只隐藏界面,setVisible(false)。
3.this.setDefaultCloseOperation(2);//DISPOSE_ON_CLOSE,隐藏并释放窗体,dispose(),当最后一个窗口被释放后,则程序也随之运行结束。
4.this.setDefaultCloseOperation(3);//EXIT_ON_CLOSE,直接关闭应用程序,System.exit(0)。一个main函数对应一整个程序。
因此,默认情况下,关闭窗口,只隐藏界面,不释放占用的内存。
点击窗口右上角关闭,四种关闭方式:
1.this.setDefaultCloseOperation(0);// DO_NOTHING_ON_CLOSE,不执行任何操作。
2.this.setDefaultCloseOperation(1);//HIDE_ON_CLOSE,只隐藏界面,setVisible(false)。
3.this.setDefaultCloseOperation(2);//DISPOSE_ON_CLOSE,隐藏并释放窗体,dispose(),当最后一个窗口被释放后,则程序也随之运行结束。
4.this.setDefaultCloseOperation(3);//EXIT_ON_CLOSE,直接关闭应用程序,System.exit(0)。一个main函数对应一整个程序。