Java JOptionPane 不会在其他窗口的顶部显示其对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/438463/
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
JOptionPane won't show its dialog on top of other windows
提问by d1ck50n
I have problem currently for my swing reminder application, which able to minimize to tray on close. My problem here is, I need JOptionPane dialog to pop up on time according to what I set, but problem here is, when I minimize it, the dialog will pop up, but not in the top of windows when other application like explorer, firefox is running, anyone know how to pop up the dialog box on top of windows no matter what application is running?
我的摆动提醒应用程序目前有问题,它能够在关闭时最小化到托盘。我的问题是,我需要根据我的设置准时弹出 JOptionPane 对话框,但这里的问题是,当我最小化它时,对话框会弹出,但当其他应用程序如资源管理器,firefox 时,不会在窗口顶部弹出正在运行,有谁知道无论运行什么应用程序,如何在窗口顶部弹出对话框?
回答by Markus Lausberg
Windows is blocking this operation since XP.
自 XP 起,Windows 就阻止了此操作。
The scenario before was like: Your a tiping in some text in an editor and not recognize that another dialog is coming to front when you are tipping the text. The coming dialog gets the focus and you are tiping in the new dialog. Maybe you click enter after you are ready and do this in the wrong dialog, which is asking whether you realy want to delet your hard disk ;)
之前的场景是这样的:您在编辑器中对某些文本进行了提示,并且在您提示文本时没有意识到另一个对话框出现在前面。即将到来的对话获得焦点,您正在新对话中小费。也许您在准备好后单击 Enter 并在错误的对话框中执行此操作,该对话框询问您是否真的要删除硬盘;)
The come to front call in java is only working for java windows.
java 中的前台调用仅适用于 java windows。
The possibibilty to notify the user of a new window is to implement a Frame, which will highlighted/flashing in the windows task bar.
通知用户新窗口的可能性是实现一个框架,它将在窗口任务栏中突出显示/闪烁。
回答by ShawnD
You might think about using a JFrame instead. It may give you a little more flexibility.
您可能会考虑改用 JFrame。它可能会给你更多的灵活性。
If you are using a JFrame and you want it to popup on top of the other windows use:
如果您使用的是 JFrame 并且希望它在其他窗口之上弹出,请使用:
myFrame.setVisible(true);
myFrame.setState(Frame.NORMAL);
The setState will show the window to the user if it was in minimized state previously.
如果窗口之前处于最小化状态,则 setState 将向用户显示该窗口。
回答by ShawnD
Are you using one of the canned JOptionPanes? (Like JOptionPane.showCOnfirmDialog(...))
您使用的是罐装 JOptionPanes 之一吗?(像 JOptionPane.showCONfirmDialog(...))
You may want to look at extending JDialog and making your own dialog panel, and then calling myDialog.setAlwaysOnTop(true);
您可能想查看扩展 JDialog 并制作自己的对话框面板,然后调用 myDialog.setAlwaysOnTop(true);
回答by d1ck50n
Correction the post above..
更正上面的帖子..
I have resolve my problem as below:
我已经解决了我的问题如下:
this.setVisible(true); // show main frame
MyDialog dialog = New MyDialog(this, true); // show my custom dialog
dialog.setVisible(true);
this.setVisible(false);
it works fine for me :)
这对我来说可以 :)
回答by Walter
Create an empty respectively dummy JFrame, set it always on top and use it as the component for the JOptionPane instead of null. So the JOptionPane remains always on top over all other windows of an application. You can also determine where the JOptionPane appears on screen with the location of the dummy JFrame.
创建一个分别为空的虚拟 JFrame,将其始终设置在顶部并将其用作 JOptionPane 而不是 null 的组件。因此 JOptionPane 始终位于应用程序的所有其他窗口之上。您还可以使用虚拟 JFrame 的位置来确定 JOptionPane 在屏幕上的显示位置。
JFrame frmOpt; //dummy JFrame
private void question() {
if (frmOpt == null) {
frmOpt = new JFrame();
}
frmOpt.setVisible(true);
frmOpt.setLocation(100, 100);
frmOpt.setAlwaysOnTop(true);
String[] options = {"delete", "hide", "break"};
int response = JOptionPane.showOptionDialog(frmOpt, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "delete");
if (response == JOptionPane.YES_OPTION) {
removeRow();
}
frmOpt.dispose();
}
回答by Jacques van Brakel
Old post, but I was struggling with this.
旧帖子,但我正在努力解决这个问题。
My problem was more with Javafx allowing the JOptionPane to go behind the current Java window.
我的问题更多是 Javafx 允许 JOptionPane 位于当前 Java 窗口的后面。
Therefore I used the following which does what the original poster asked by putting the JOptionPane in front of all windows; even JAVAFX.
因此,我使用了以下内容,通过将 JOptionPane 放在所有窗口前面来完成原始海报的要求;甚至 JAVAFX。
Firstly the old JOptionPane:
首先是旧的 JOptionPane:
JOptionPane.showMessageDialog(null, "Here I am");
Now an JOptionPane that stays in front:
现在是一个位于前面的 JOptionPane:
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Here I am");
And for fun here is everything in one long line:
为了好玩,这里的所有内容都排成一排:
JOptionPane.showMessageDialog(
((Supplier<JDialog>) () -> {final JDialog dialog = new JDialog(); dialog.setAlwaysOnTop(true); return dialog;}).get()
, "Here I am");
You can make a static method some where that will return the JDialog for you and then just call it in the JOptionPane to clean up your code a bit.
您可以在某些地方创建一个静态方法,该方法将为您返回 JDialog,然后只需在 JOptionPane 中调用它以稍微清理您的代码。