我可以以非模态方式使用Java JOptionPane吗?

我正在开发一个应用程序,当某个动作发生时弹出一个JOptionPane。 我只是想知道当JOptionPane确实弹出时你怎么可能仍然使用后台应用程序。 目前,当JOptionPane弹出时,我不能做任何其他事情,直到我关闭JOptionPane。

编辑

感谢回复人员和信息。 认为生病了将此function从应用程序中删除,因为它看起来可能比必要的更麻烦。

文档明确指出,通过showXXXDialog方法创建时, 所有对话框都是模态的 。

您可以使用的是从文档中获取的直接使用方法以及JDialog从Dialoginheritance的setModal方法:

JOptionPane pane = new JOptionPane(arguments); // Configure via set methods JDialog dialog = pane.createDialog(parentComponent, title); // the line below is added to the example from the docs dialog.setModal(false); // this says not to block background components dialog.show(); Object selectedValue = pane.getValue(); if(selectedValue == null) return CLOSED_OPTION; //If there is not an array of option buttons: if(options == null) { if(selectedValue instanceof Integer) return ((Integer)selectedValue).intValue(); return CLOSED_OPTION; } //If there is an array of option buttons: for(int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) { if(options[counter].equals(selectedValue)) return counter; } return CLOSED_OPTION; 

您应该可以在这里获得更多信息: http : //download.oracle.com/javase/tutorial/uiswing/components/dialog.html

对话可以是模态的。 当modal dialog可见时,它会阻止用户输入程序中的所有其他窗口。 JOptionPane创建模态的JDialog。 要创建非modal dialog,必须直接使用JDialog类。

从JDK6开始,您可以使用新的Modality API修改Dialog窗口模态行为。 有关详细信息,请参阅新模态API。

在你的Java应用程序中,我认为你运气不好:我没有检查过,但我认为JOptionPane的showXXXDialog方法会弹出一个所谓的modal dialog,它使同一个JVM的其余GUI保持不活动状态。

但是,Java没有任何前瞻性超能力:你仍然可以使用Alt-Tab到其他(非Java)应用程序。

这个简单的调整对我有用(1.6+)。 用四行代码替换showXXXDialog:(1)创建一个JOptionPane对象(2)调用其createDialog()方法获取JDialog对象(3)将JDialog对象的模态类型设置为无模式(4)设置可见性JDialog的真实性。