java 如何确保 JDialog 始终处于领先地位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2632012/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:07:32  来源:igfitidea点击:

how to ensure that JDialog always stays on top

javavisibilityjdialog

提问by mithun1538

I have a JDialogthat takes a name from the user. Behind the JDialog, is an applet. I dont want the user to access that applet until he has entered the name. I tried JDialog.setAlwaysOnTop(true), but the applet throws an AccessExceptionerror. So what I did was keep a while loop that will execute JDialog.setVisible(true)till the JtextField(input for user name) is empty (""). But for some reason this works really slow, meaning the JDialogloads, but it takes time to focus on the JTextFieldand even when the user types his name, it comes really slow... like one character in 2 seconds... Is there any other way for me to force the user to enter the name before accessing the applet?

我有一个JDialog从用户那里取名字的。在JDialog,后面是一个小程序。我不希望用户在输入名称之前访问该小程序。我试过了JDialog.setAlwaysOnTop(true),但小程序抛出AccessException错误。所以我所做的是保持一个while循环,该循环将执行JDialog.setVisible(true)直到JtextField(用户名的输入)为空(“”)。但由于某种原因,这工作真的很慢,这意味着JDialog负载,但需要时间来专注于JTextField,即使当用户输入他的名字时,它也很慢......就像一个字符在 2 秒内......还有其他的吗?强制用户在访问小程序之前输入名称的方法?

回答by camickr

Use a modal JDialog. For example the code in your init(...) method of JApplet might include:

使用模态 JDialog。例如,JApplet 的 init(...) 方法中的代码可能包括:

JDialog dialog = new JDialog(SwingUtilities.windowForComponent(this));
dialog.setModal(true);
dialog.setSize(...);
dialog.setVisible( true );

Or you can just use a JOptionPane.showInputDialog(). Again you would just specify "this" as the parent component of the option pane.

或者您可以只使用 JOptionPane.showInputDialog()。同样,您只需将“this”指定为选项窗格的父组件。

回答by Bogdan M.

Another option would be:

另一种选择是:

frame.setAlwaysOnTop(true);

It forces the dialog on top of any other.

它强制对话框位于任何其他对话框之上。

回答by OscarRyz

It runs slowly because the program is processing that foo loop

它运行缓慢,因为程序正在处理该 foo 循环

What you can do is to add a window listener and then the jdialog lost it's focus ( or the applet gains it ) return the focus to the jdialog.

您可以做的是添加一个窗口侦听器,然后 jdialog 失去焦点(或小程序获得它)将焦点返回到 jdialog。

This should perform much better than the for loop you're using right now

这应该比你现在使用的 for 循环表现得更好