JFrame窗口关闭方式

世界杯积分规则 2025-10-31 10:47:15 3052

Java JFrame窗口关闭方法总结

public void setDefaultCloseOperation(int operation):

默认关闭状态:JFrame.class中: private int defaultCloseOperation =HIDE_ON_CLOSE;

因此,默认情况下,关闭窗口,只隐藏界面,不释放占用的内存。

点击窗口右上角关闭,四种关闭方式:

1.this.setDefaultCloseOperation(0);// WindowConstants.DO_NOTHING_ON_CLOSE,不执行任何操作。

2.this.setDefaultCloseOperation(1);//WindowConstants.HIDE_ON_CLOSE,只隐藏界面,setVisible(false)。

3.this.setDefaultCloseOperation(2);//WindowConstants.DISPOSE_ON_CLOSE,隐藏并释放窗体,dispose(),当最后一个窗口被释放后,则程序也随之运行结束。

4.this.setDefaultCloseOperation(3);//WindowConstants.EXIT_ON_CLOSE,直接关闭应用程序,System.exit(0)。一个main函数对应一整个程序。

站点统计