我可以以非模态方式使用 Java JOptionPane 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5706455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use a Java JOptionPane in a non-modal way?
提问by yemyem
I am working on an application which pops up a JOptionPane when a certain action happens. I was just wondering if it was possible that when the JOptionPane does pop up how can you still use the background applications. Currently when the JOptionPane pops up I can't do anything else until I close the JOptionPane.
我正在开发一个应用程序,当某个操作发生时它会弹出一个 JOptionPane。我只是想知道是否有可能当 JOptionPane 弹出时你仍然可以使用后台应用程序。目前,当 JOptionPane 弹出时,在关闭 JOptionPane 之前我不能做任何其他事情。
EDIT
编辑
Thanks for the reply guys and the information. Think ill leave this function out of the application cause it looks like it could be more hassle than necessary.
谢谢大家的回复和信息。认为将此功能排除在应用程序之外,因为它看起来可能比必要的麻烦。
采纳答案by justkt
The documentation explicitly states that all dialogs are modalwhen created through the showXXXDialog methods.
该文档明确指出所有对话框在通过 showXXXDialog 方法创建时都是模态的。
What you can use is the Direct Use method taken from the docs and the setModalmethod that JDialog inherits from Dialog:
您可以使用的是从文档中获取的 Direct Use 方法和JDialog 从 Dialog 继承的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;
回答by yavoh
You should be able to get more information here: http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html
您应该可以在此处获得更多信息:http: //download.oracle.com/javase/tutorial/uiswing/components/dialog.html
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.
Starting with JDK6, you can modify Dialog window modality behavior using the new Modality API. See The New Modality API for details.
对话框可以是模态的。当模态对话框可见时,它会阻止用户输入到程序中的所有其他窗口。JOptionPane 创建模态的 JDialog。要创建非模态对话框,您必须直接使用 JDialog 类。
从 JDK6 开始,您可以使用新的 Modality API修改 Dialog 窗口模态行为 。有关详细信息,请参阅新模式 API。
回答by Carl Smotricz
Within your Java application, I think you're out of luck: I haven't checked, but I think that JOptionPane's showXXXDialog methods pop up a so-called modal dialog that keeps the rest of the GUI from the same JVM inactive.
在您的 Java 应用程序中,我认为您不走运:我没有检查过,但我认为 JOptionPane 的 showXXXDialog 方法会弹出一个所谓的模式对话框,使来自同一 JVM 的 GUI 的其余部分保持不活动状态。
However, Java doesn't have any foreground-grabbing super powers: You should still be able to Alt-Tab to other (non-Java) applications.
但是,Java 没有任何抢占前台的超能力:您仍然应该能够将 Alt-Tab 键用于其他(非 Java)应用程序。
回答by Pete Sanderson
This easy tweak worked for me (1.6+). Replaced the showXXXDialog with four lines of code to: (1) create a JOptionPane object (2) call its createDialog() method to get a JDialog object (3) set the modality type of the JDialog object to modeless (4) set the visibility of the JDialog to true.
这个简单的调整对我有用(1.6+)。将showXXXDialog替换为四行代码:(1)创建JOptionPane对象(2)调用其createDialog()方法获取JDialog对象(3)设置JDialog对象的模态类型为无模式(4)设置可见性JDialog 为 true。