简体   繁体   English

JFrame 即使在关闭后仍保持应用程序运行

[英]JFrame keeps application running even after closing

I have a class with only static methods and one of them opens a JOptionPane error message dialogue using a JFrame object as component.我有一个 class ,只有 static 方法,其中一个使用 JFrame2static 方法打开 JOptionPane 错误消息对话框。

This is the class + method:这是 class + 方法:

public class miscMethods
{
    static JFrame errorWindow = null;

    public static void ErrorPopup(String message)
    {
        errorWindow = new JFrame();
        errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        errorWindow.setAlwaysOnTop(true);
        JOptionPane.showMessageDialog(errorWindow, message, "Error", JOptionPane.ERROR_MESSAGE);
        errorWindow = null;
    }
}

The ErrorPopup method is used inside a JavaFX controller and other places, called like this: ErrorPopup 方法在一个 JavaFX controller 等地方使用,调用如下:

import static code.miscMethods.ErrorPopup;
...
ErrorPopup("This is the error message");

Problem is that the application's process won't close when I close the the program from the window's ✕ after the popup appears, because the JFrame was created and shown.问题是当我在弹出窗口出现后从窗口的✕关闭程序时,应用程序的进程不会关闭,因为 JFrame 已创建并显示。 I know the JFrame is the culprit, so I added the errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);我知道 JFrame 是罪魁祸首,所以我添加了errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); but it doesn't seem to do anything, since the program isn't closing.但它似乎没有做任何事情,因为程序没有关闭。

In this question: JFrame and why stay running The accepted answer talks about non-daemon threads, but the only thread I open is a daemon one, so unless JavaFX open one then it can't be that I believe.在这个问题中: JFrame 以及为什么要继续运行接受的答案谈到非守护线程,但我打开的唯一线程是守护线程,所以除非 JavaFX 打开一个,否则我相信它不可能是。

So, why does the process keep running and how can I solve it?那么,为什么该过程会继续运行,我该如何解决呢?

I'm still new to Java so if I made a mistake and/or my code shows bad practices please do point them out!我还是 Java 的新手,所以如果我犯了错误和/或我的代码显示了不好的做法,请指出!

Edit: I'm using a JFrame because I need the setAlwaysOnTop, since using JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);编辑:我使用的是 JFrame 因为我需要 setAlwaysOnTop,因为使用JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE); opens it not on top of the JavaFX window.打开它不在 JavaFX window 之上。 If there's a better way let me know.如果有更好的方法请告诉我。

This:这个:

errorWindow = null;

does nothing of use since the object is still displayed.没有任何用处,因为 object 仍然显示。 You want this instead:你想要这个:

errorWindow.dispose();

Actually, even better, simply get rid of errorWindow altogether and pass null as the first parameter to the JOptionPane.实际上,更好的是,完全摆脱errorWindow并将null作为第一个参数传递给 JOptionPane。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM