由于在写程序中涉及到窗口关闭的多种情况的选择运用,所以,对窗口的四种关闭情况,做一下整理。
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函数对应一整个程序。
以下代码中,可看出每种关闭方式对应的操作:
具体是如何处理的,就要看底层了,请各位高人多多指教。
测试代码:主窗口:
由主窗口产生的副窗口:
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函数对应一整个程序。
以下代码中,可看出每种关闭方式对应的操作:
- protected void processWindowEvent(WindowEvent e) {
- super.processWindowEvent(e);
- if (e.getID() == WindowEvent.WINDOW_CLOSING) {
- switch(defaultCloseOperation) {
- case HIDE_ON_CLOSE:
- setVisible(false);
- break;
- case DISPOSE_ON_CLOSE:
- dispose();
- break;
- case DO_NOTHING_ON_CLOSE:
- default:
- break;
- case EXIT_ON_CLOSE:
- // This needs to match the checkExit call in
- // setDefaultCloseOperation
- System.exit(0);
- break;
- }
- }
- }
测试代码:主窗口:
- package XMLClient3;
- import java.awt.event.ActionEvent;
- //测试setDefaultCloseOperation
- public class Test extends javax.swing.JFrame{
- public static void main(String args[]){
- Test tt=new Test();
- tt.initUI();
- }
- //初始化界面
- public void initUI(){
- this.setTitle("登录");
- this.setSize(300,200);
- this.setLayout(new java.awt.FlowLayout());//设置布局管理方式
- this.setLocationRelativeTo(null);
- //登陆按钮
- javax.swing.JButton butLogin=new javax.swing.JButton("登录");
- this.add(butLogin);
- //注册按钮
- javax.swing.JButton butRegister=new javax.swing.JButton("注册");
- this.add(butRegister);
- final Test1 tt=new Test1();
- //按钮监听器
- java.awt.event.ActionListener al=new java.awt.event.ActionListener(){
- //登陆事件
- public void actionPerformed(ActionEvent e) {
- String cmd=e.getActionCommand();//得到动作事件
- if("登录".equals(cmd)){
- System.out.println("点击了登陆按钮");
- tt.initUI();
- Test1.Runningstate=true;//控制run中的while循环
- // //将Runnable对象tt包装成Thread对象t
- Thread t=new Thread (tt);
- t.start();
- }else if("注册".equals(cmd)){
- System.out.println("点击了注册按钮");
- tt.setVisible(true);
- if(false==Test1.Runningstate)//测试Tset1中的监听是否有效
- System.out.println("false==Test1.Runningstate");
- }
- }
- };
- butLogin.addActionListener(al);
- butRegister.addActionListener(al);
- //this.setDefaultCloseOperation(0);//点击右上角,DO_NOTHING_ON_CLOSE,不执行任何操作。
- //this.setDefaultCloseOperation(1);//HIDE_ON_CLOSE,只隐藏界面,setVisible(false)
- this.setDefaultCloseOperation(2);//DISPOSE_ON_CLOSE,隐藏并释放窗体。当最后一个窗口被释放后,则程序也随之运行结束。
- //this.setDefaultCloseOperation(3);//EXIT_ON_CLOSE,直接关闭应用程序System.exit(0);
- this.setVisible(true);
- }
- }
由主窗口产生的副窗口:
- package XMLClient3;
- import java.awt.event.WindowEvent;
- public class Test1 extends javax.swing.JFrame implements Runnable{
- static boolean Runningstate=false;
- //初始化界面
- public void initUI(){
- this.setTitle("登录主界面之后的登录副界面");
- this.setSize(300,200);
- this.setLayout(new java.awt.FlowLayout());//设置布局管理方式
- this.setLocationRelativeTo(null);
- //this.setDefaultCloseOperation(0);//不做任何事DO_NOTHING_ON_CLOSE。//windowClosed(WindowEvent e),监听无效。
- //this.setDefaultCloseOperation(1);//HIDE_ON_CLOSE,只隐藏界面,程序仍在运行。setVisible(false)。//窗口关闭,监听无效。
- this.setDefaultCloseOperation(2);//DISPOSE_ON_CLOSE,隐藏并释放窗体。当最后一个窗口被释放后,则程序也随之运行结束。//窗口关闭,监听有效。
- //this.setDefaultCloseOperation(3);//EXIT_ON_CLOSE,关闭应用程序System.exit(0);//窗口关闭,监听有效。
- this.setVisible(true);
- //Windows界面监听器
- this.addWindowListener(new java.awt.event.WindowListener(){
- public void windowClosed(WindowEvent e) {//点击右上角关闭时,退出线程run中while循环
- Runningstate=false;
- System.out.println("===================================================");
- }
- public void windowClosing(WindowEvent e) {
- }
- public void windowActivated(WindowEvent e) {
- }
- public void windowDeactivated(WindowEvent e) {
- }
- @Override
- public void windowDeiconified(WindowEvent e) {
- }
- public void windowIconified(WindowEvent e) {
- }
- public void windowOpened(WindowEvent e) {
- }
- });
- }
- public void run(){//若不用线程,则while一直在此执行,无法执行其他操作语句
- while(Runningstate){
- System.out.println("不断执行中");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- 本文转自:http://bigfang.iteye.com/blog/942392