[英]Set Java Swing Input Dialog Always on top
If I need to make my JOptionPane
always on the top of everything, this is the way I can do it: 如果我需要让我的JOptionPane
始终处于最重要的位置,那么我就可以这样做:
JOptionPane optionPane = new JOptionPane();
JDialog dialog = optionPane.createDialog("My Dialog");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
But what about the input dialog of JOptionPane
? 但是JOptionPane
的输入对话框怎么样? Can we do similar thing to make sure the below input dialog appear on top? 我们可以做类似的事情以确保下面的输入对话框出现在顶部吗?
JOptionPane.showInputDialog(null,"My Input");
Please note my application is not a GUI one, so I cannot set the parent window. 请注意我的应用程序不是GUI,因此我无法设置父窗口。
Finally I found the solution by digging down the JOptionPane
class itself. 最后,我通过挖掘JOptionPane
类本身找到了解决方案。
JOptionPane
has a method called setWantsInput()
which we need to set true
, to make it a input dialog. JOptionPane
有一个名为setWantsInput()
的方法,我们需要将其设置为true
,以使其成为输入对话框。
Here is a working code: 这是一个有效的代码:
JOptionPane optionPane = new JOptionPane("body of the dialog");
optionPane.setWantsInput(true); //this is what I added
JDialog dialog = optionPane.createDialog("Title Of the Dialog");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
dialog.dispose();
System.out.println(optionPane.getInputValue());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.