如何阻止 JOptionPane 禁用根框架

How to stop JOptionPane from disabling root frame

如何在不禁用任何当前打开的框架的情况下显示 JOptionPane?

我有一个带有开始和停止按钮的 JFrame。在任何时候,我都希望用户能够按下主框架上的 'Stop' 按钮,以停止辅助线程(由“开始”按钮启动)。

但是第二个 运行 线程有时会打开一个 JOptionPane。当它这样做时,主框架被禁用,用户不能按停止按钮。

(当它连续打开多个 JOptionPanes 时,试图停止它会变得非常令人沮丧)。

我试过了

JOptionPane.showMessageDialog(null, "It's Broken Mate");

没有成功。我还尝试将 JFrame 传递给它以禁用:

JOptionPane.showMessageDialog(new JFrame(), "Still No Go");

那也失败了。我什至试过

JFrame frame = new JFrame();
frame.setVisible(true);
JOptionPane.showMessageDialog(frame, "CMON Please");

而且,

JFrame frame = new JFrame();
frame.setVisible(true);
JOptionPane.setRootFrame(frame);
JOptionPane.showMessageDialog(frame, "I'm getting angry now.");

而且还在。没有什么。不起作用。它打开一个消息框,一切都被禁用。在再次启用任何内容之前,我必须关闭 JOptionPane...

有什么想法吗?

干杯

JOptionPanemodal,所以你不能直接这样做。

All dialogs are modal. Each showXxxDialog method blocks the caller until the user's interaction is complete.

您必须使用 JDialog 或类似的。

    JOptionPane p = new JOptionPane("I'm getting happy now.");
    JDialog k = p.createDialog(":-)");
    k.setModal(false); // Makes the dialog not modal
    k.setVisible(true);