Java对话框不处理 [英] Java dialog does not dispose

查看:87
本文介绍了Java对话框不处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java noob在这里。当用户按下Windows关闭按钮时,扩展了 JDialog 的Swing类不会处理-java.exe保留在内存中。我将代码剥离到了这个外壳上,仍然可以得到这种行为。

Java noob here. My Swing class that extends JDialog does not dispose when the user presses the Windows Close button - java.exe stays in memory. I've stripped the code right down to this shell, I still get this behaviour.

我查看了其他示例,例如基本Java Swing,如何退出和处理应用程序/ JFrame

当我注释掉该示例代码中的两个 System.exit(0)行时,该示例中的类仍然可以正确处理。我想让我的班级丢掉什么?

I took a look at other samples, such as at Basic Java Swing, how to exit and dispose of your application/JFrame
When I commented out the two System.exit(0) lines in that sample code, the class in that sample still disposed correctly. What am I missing to make my class dispose?

import javax.swing.JFrame;
import javax.swing.JDialog;
public class WhyNoDispose extends JDialog{
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    WhyNoDispose frame = new WhyNoDispose("my title");
                    frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    frame.setVisible(true);
                    //System.exit(0);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public WhyNoDispose(String title) {
        super(new JFrame(title), ModalityType.APPLICATION_MODAL);
        pack();
    }
}


推荐答案

您正在创建一个JFrame,并且从不将其放置在这里:

You're creating a JFrame and never disposing it here:

public WhyNoDispose(String title) {
    super(new JFrame(title), ModalityType.APPLICATION_MODAL); // *********
    pack();
}

因此,由于JFrame处于活动状态并且GUI已呈现,因此Swing事件线程继续运行。

So since the JFrame is alive and a GUI has been rendered, the Swing event thread keeps on running.

如果改为使JFrame起作用,以便在JFrame关闭时退出程序,然后显式处置JFrame,则程序现在退出:

If you instead make the JFrame behave so that the program exits on JFrame close, and then explicitly dispose of the JFrame, your program now exits:

import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JDialog;

public class WhyNoDispose extends JDialog {
   public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            try {
               WhyNoDispose frame = new WhyNoDispose("my title");
               frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
               JFrame win = (JFrame) frame.getOwner();
               win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
               win.dispose();
               // System.exit(0);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }

   public WhyNoDispose(String title) {
      super(new JFrame(title), ModalityType.APPLICATION_MODAL);
      pack();
   }
}

但这至少是很混乱的代码-如果拥有的窗口不是JFrame怎么办?

But this is very kludgy code, to say the least -- what if the owning window isn't a JFrame? What if it's null?

另一种解决方案是根本不使用JFrame,这样在处置JDialog时,就不会再留有任何持久窗口来使事件线程持久化了。 :

Another solution is to use no JFrame at all, so that when the JDialog is disposed, there's no persisting window left over to make the event thread persist:

import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JDialog;

public class WhyNoDispose extends JDialog {
   public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            try {
               WhyNoDispose frame = new WhyNoDispose("my title");
               frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
               frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }

   public WhyNoDispose(String title) {
      super((JFrame)null, title);
      pack();
   }
}

这篇关于Java对话框不处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆