JDialog 位于其 JFrame 父级前面
我想在 Java Swing 中创建一个 JDialog,当它打开时,无法访问其父窗口(就像在 Microsoft Word 中打开文件资源管理器对话框一样)。 JDialog 类中是否有提供此行为的方法?
I want to create in Java Swing a JDialog which, when it's open, its parent window cannot be accessed (just like when you open the file explorer dialog in Microsoft Word). Is there any method in the JDialog class that provides this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如何将 JDialog 锁定在他的 JFrame Parent 中?
确实,使用 JDialog.setModal 能够使 jdialog 就像其他应用程序上的对话框一样。在 jDialog 关闭之前停止所有框架
how about to lock JDialog inside his JFrame Parent ?
it's true that using JDialog.setModal capable to making jdialog just like dialog on other application. stopped all frame bofore jDialog closed
在设置对话框可见之前使用 JDialog.setModal(true)
use JDialog.setModal(true) before setting dialog visible
您有两个选择:
使用
JOptionPane
中的静态方法。这些将默认创建模式对话框:显式创建一个
JDialog
:第一个选项要简单得多,我倾向于更喜欢它,特别是对于模式对话框。
You have two options:
Use the static methods in
JOptionPane
. These will create modal dialogs by default:Create a
JDialog
explicitly:The first option is far simpler and I tend to prefer it particularly with modal dialogs.
Adamski 和 Jan 都已经有了正确的答案,但我只想确保解释了模态窗口的概念。
OP 询问了一个阻止访问父级的对话框。这称为模式对话框(或模式窗口)。维基百科给出了这个定义:
因此,“模态”意味着它将阻塞父窗口(用户无法与除模态窗口之外的任何窗口交互)窗口),而“非模式”或“无模式”意味着子窗口和父窗口将同时访问。
这是一般 GUI 框架中都存在的概念,而不仅仅是 Swing 框架。在您使用的任何 GUI 框架中,您可能可以通过查找
modal
属性来找到这种功能。Adamski and Jan both have the correct answers already, but I wanted to just make sure that the concept of a modal window is explained.
The OP asked about a dialog that blocks access to the parent. This is called a modal dialog (or a modal window). Wikipedia gives this definition:
So, "modal" means that it will block parent windows (users cannot interact with any window besides the modal window), while "non-modal", or "modeless" means that the child and parent windows will be accessible at the same time.
This is a concept that exists in GUI frameworks in general, not just the Swing framework. In any GUI framework that you use, you can probably find this kind of functionality by looking for a
modal
property.