如何确保JDialog始终保持在最前面
我有一个从用户那里获取名称的 JDialog
。 JDialog
后面是一个小程序。我不希望用户在输入名称之前访问该小程序。我尝试了 JDialog.setAlwaysOnTop(true)
,但小程序抛出 AccessException
错误。因此,我所做的是保持一个 while 循环,该循环将执行 JDialog.setVisible(true) 直到 JtextField(用户名输入)为空 ("")。但由于某种原因,它的运行速度非常慢,这意味着 JDialog
加载,但需要时间来关注 JTextField
,甚至当用户输入他的名字时,它也非常慢...就像 2 秒内出现一个字符...我还有其他方法可以强制用户在访问小程序之前输入名称吗?
I have a JDialog
that 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 AccessException
error. 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 JDialog
loads, but it takes time to focus on the JTextField
and 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用模态 JDialog。例如,JApplet 的 init(...) 方法中的代码可能包括:
或者您可以只使用 JOptionPane.showInputDialog()。同样,您只需将“this”指定为选项窗格的父组件。
Use a modal JDialog. For example the code in your init(...) method of JApplet might include:
Or you can just use a JOptionPane.showInputDialog(). Again you would just specify "this" as the parent component of the option pane.
另一种选择是:
它强制对话框位于任何其他对话框之上。
Another option would be:
It forces the dialog on top of any other.
它运行缓慢,因为程序正在处理 foo 循环
您可以做的是添加一个窗口侦听器,然后 jdialog 失去焦点(或小程序获得焦点)将焦点返回给 jdialog。
这应该比您现在使用的 for 循环执行得更好
It runs slowly because the program is processing that foo loop
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.
This should perform much better than the for loop you're using right now