Java: 在另一个线程中执行 showOptionDialog 然后退出它会关闭整个应用程序

Java: executing a showOptionDialog in another Thread and then exiting from it makes closing the entire application

我在另一个线程中 运行 实现了以下代码 Java 一个带有摆动的对话事件,而我的程序正在做这些事情。

public class othermain implements Runnable {

    public void displayDialog() {
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Insert text");
        TextField text = new TextField(15);
        panel.add(label);
        panel.add(text);
        String[] options = new String[]{"Cancel", "Ok"};
        int option = JOptionPane.showOptionDialog(null, panel, "Ask",
                JOptionPane.NO_OPTION, JOptionPane.NO_OPTION,
                null, options, options[1]);
        if (option == 1) {
            System.out.println(text.getText());

        }
    }

    @Override
    public void run() {
        this.displayDialog();
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        othermain a = new othermain();
        //a.load();
        Thread th = new Thread(a);
        th.start();

        while (true) {
            System.out.println("I should never exit from the cycle");
            Thread.sleep(3000);
        }
    }
}

这行得通,但问题是在 macOS 上,一旦打开对话框,程序的图标就会保留在 dock 中,所以当我尝试关闭它时,即使对话框已在中执行,我的整个应用程序也会关闭另一个线程。换句话说,我预计只有对话执行的线程应该在按下确定按钮或取消按钮后关闭。 如何避免描述的行为并隐藏图标,只显示消息框而不显示程序图标,以便无法手动关闭它?或者,如果在手动退出应用程序时无法避免这种情况,则应仅关闭正在执行它的线程而不是整个应用程序。

当前输出:

I should never exit from the cycle
I should never exit from the cycle
I should never exit from the cycle
text
I should never exit from the cycle

Process finished with exit code 0 //When I quit the application from the dock

对于那些想知道这个问题的人,我用这个技巧解决了。

System.setProperty("apple.awt.UIElement", "true");
java.awt.Toolkit.getDefaultToolkit();

主要更新:

//Code for messagebox is the same 

public static void main(String[] args) throws IOException, InterruptedException {
    System.setProperty("apple.awt.UIElement", "true");
    java.awt.Toolkit.getDefaultToolkit();
    othermain a = new othermain();
    a.displayDialog();
    //Thread th = new Thread(a); //You don't need to run in a separated thread now
    //th.start();
    while (true) {
        System.out.println("I should never exit from the cycle");
        Thread.sleep(3000);
    }
}

特别感谢让我“遵守Java约定”的用户没有添加任何关于问题的额外词。他帮了我很多,就像很多像他一样回答的用户。