如何在其他弹出窗口上显示弹出窗口?
在我的应用程序中,在某个屏幕上,我启动了一个弹出窗口。根据用户将在此弹出窗口上单击的按钮,必须启动另一个弹出窗口。我使用 JDialog 对象来实现这些弹出窗口。问题是第二个弹出窗口没有显示(即使使用 setVisible(true) 和 toFront())。它已创建,但我看不到它。我在其构造函数中将第一个弹出窗口定义为其所有者。有人可以帮忙吗?
In my application, on some screen, I launch a popup. Depends on what button the user will click on this popup, another popup has to be launched. I use JDialog object to implement these popups. The issue is that the second popup doesn't show up (even though is use setVisible(true) and toFront()). It's created but I can't see it. I defined in its constructor the first popup as its owner. Anyone can help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当从父窗口或对话框打开 JDialog 并将其设置为模态时,父窗口的事件调度线程将停止。这可以防止父级集中注意力或传递其他事件,或者本质上在模式对话框关闭之前执行任何操作。因此,该调用被阻塞。
您必须做的是从其他地方触发事件,例如新对话框而不是父窗口,或者不使用模式对话框,而是使用常规
JFrame
并将其设置为始终位于顶部setAlwaysOnTop(true)
。这意味着用户可以继续使用父窗口,并且事件仍将从其触发。附录:针对您的问题“程序集中于显示它,并且不对必须隐藏它的事件做出反应”:当您制作对话框模式时,一旦使其可见,它就会阻塞父窗口直到它关闭,包括事件触发。如果您需要以编程方式关闭新的弹出窗口,则需要使弹出窗口成为非模态窗口,或者需要在新的弹出窗口的上下文中执行后续代码(例如当它变得可见时触发一个事件)
When a
JDialog
is opened from a parent window or dialog, and set to be modal, the Event Dispatch Thread for the parent window is halted. This prevents the parent from being focused or from passing other events, or essentially doing anything until the modal dialog is closed. The call is therefore blocking.What you must do instead is fire your event from somewhere else, like the new dialog instead of the parent window, OR instead of using modal dialogs, use a regular
JFrame
and set it to be always on top usingsetAlwaysOnTop(true)
. That means the user can continue to use the parent window, and events will still fire from it.Addendum: in response to your problem "the program concentrates in showing it and doesn't react to the event that has to hide it": when you make a dialog modal, as soon as you make it visible, it will block the parent window until it is closed, including event firing. If you need the new popup to be closed programatically, you either need to make the popup non-modal, or you need to execute the subsequent code in the context of the new popup window (such as firing an event when it becomes visible)
好的,现在我成功地显示了第二个弹出窗口。触发弹出窗口的事件中的代码是:
但现在我有一个不同的问题:
当printingWindow 设置为可见时,程序集中于显示它,并且不会对必须隐藏它的事件做出反应。
触发适当的事件时执行的代码是:
那么我如何关闭此弹出窗口(通过触发事件)?
OK, now I managed to show the second popup. The code in the event that triggers the popup is:
But now I have a different problem:
when the printingWindow is set to be visible, the program concentrates in showing it and doesn't react to the event that has to hide it.
The code that is executed when the appropriate event is fired is:
So how I close this popup (by firing the event)?