JDialog 位于其 JFrame 父级前面

发布于 2024-09-19 03:54:26 字数 110 浏览 2 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海风掠过北极光 2024-09-26 03:54:27

如何将 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

花桑 2024-09-26 03:54:26

在设置对话框可见之前使用 JDialog.setModal(true)

JDialog yourdialog = ...

yourdialog.setModal(true);
...

yourdialog.setVisible(true);

use JDialog.setModal(true) before setting dialog visible

JDialog yourdialog = ...

yourdialog.setModal(true);
...

yourdialog.setVisible(true);
江挽川 2024-09-26 03:54:26

您有两个选择:

使用 JOptionPane 中的静态方法。这些将默认创建模式对话框:

Window parentWindow = SwingUtilities.getWindowAncestor(parentPanel);
JOptionPane.showMessageDialog(parentWindow, "Hello, World); // Create modal dialog aligned with parent window.

显式创建一个 JDialog

Window parentWindow = SwingUtilities.getWindowAncestor(parentPanel);
JDialog dlg = new JDialog(parentWindow, ModalityType.APPLICATION_MODAL);

第一个选项要简单得多,我倾向于更喜欢它,特别是对于模式对话框。

You have two options:

Use the static methods in JOptionPane. These will create modal dialogs by default:

Window parentWindow = SwingUtilities.getWindowAncestor(parentPanel);
JOptionPane.showMessageDialog(parentWindow, "Hello, World); // Create modal dialog aligned with parent window.

Create a JDialog explicitly:

Window parentWindow = SwingUtilities.getWindowAncestor(parentPanel);
JDialog dlg = new JDialog(parentWindow, ModalityType.APPLICATION_MODAL);

The first option is far simpler and I tend to prefer it particularly with modal dialogs.

瑾夏年华 2024-09-26 03:54:26

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:

In user interface design, a modal window is a child window that requires users to interact with it before they can return to operating the parent application

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文