简体   繁体   English

JDialog 框没有获得焦点

[英]JDialog box not gaining focus

I have a modeless dialog box being generated which prompts users to open a new window.我正在生成一个无模式对话框,提示用户打开一个新的 window。 The box can be opened in two ways, either directly from the file menu for the frame I'm writing or indirectly via the framework my panel is plugging into.可以通过两种方式打开该框,直接从我正在编写的框架的文件菜单或间接通过我的面板插入的框架。

When I make the call directly via the file menu the dialog box comes up with focus exactly as I want.当我直接通过文件菜单拨打电话时,对话框会完全按照我的意愿显示焦点。 But when I have the framework indirectly open the dialog box it does not have focus as it should.但是当我让框架间接打开对话框时,它没有应有的焦点。

There doesn't seem to be a difference between the two methods of opening the dialog, in both cases a load function is called and it's not until 5 method calls later the dialog box is opened.两种打开对话框的方法之间似乎没有区别,在这两种情况下都会调用加载 function 并且直到 5 次方法调用之后才打开对话框。 In both cases the frame which generates the dialog box is realized at the time the box is generated.在这两种情况下,生成对话框的框架都是在生成框时实现的。 I've tried calling requestFocus after making the dialog box visible but it doesn't seem to do anything.在使对话框可见后,我尝试调用 requestFocus ,但它似乎没有做任何事情。

Any suggestion why the dialog box wouldn't have focus, or how I can give it focus as a separate window from the window that usually has focus?有什么建议为什么对话框没有焦点,或者我如何将它作为一个单独的 window 与通常有焦点的 window 分开?

in some cases is hard to set Focus to the expected top-level container as are demonstrated here , but for excelent workaround would be better look at camickr's Dialog Focus在某些情况下,很难将 Focus 设置为预期的顶级容器,如此处所示,但对于出色的解决方法,最好查看 camickr 的Dialog Focus

When you create the dialog, try setting the main GUI as parent of the dialog.创建对话框时,尝试将主 GUI 设置为对话框的父级。

In the first case, when you click from menu, it automatically sets the main GUI as the parent of the dialog, but it doesnt in the second case.在第一种情况下,当您从菜单中单击时,它会自动将主 GUI 设置为对话框的父级,但在第二种情况下不会。

So make sure when you create the dialog, you are setting the main GUI/ window as parent always.因此,请确保在创建对话框时,始终将主 GUI/window 设置为父级。

It should help most times.大多数时候它应该有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM