如何在不禁用任何当前打开的框架的情况下显示 JOptionPane?
我有一个带有开始和停止按钮的 JFrame。在任何时候,我希望用户能够按下主机上的“停止”按钮,以停止辅助线程(由“开始”按钮启动)。
但是,第二个运行的线程有时会打开 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...
有什么想法吗?
干杯
最佳答案
JOptionPane
为 modal ,所以你不能直接这样做。
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);
关于java - 如何阻止 JOptionPane 禁用根框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29860863/