防止 Java Swing 中不必要的对话框重复

发布于 2024-08-19 04:09:33 字数 89 浏览 3 评论 0原文

JDialog 类中是否存在一种方法,可以防止在多次按下主窗口(JFrame)中用于打开子窗口(JDialog)的按钮时多次显示子窗口(JDialog)?非常感谢!

Does exist in the JDialog class a way to prevent that a child window (JDialog) would be displayed more than once when the button from the main window (JFrame) used to open it, is pressed several times? Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

此刻的回忆 2024-08-26 04:09:33

是的,您不需要将框设为模态来执行此操作(尽管将其设为模态将是最简单的方法)。

只需执行如下操作

在您的成员声明中:

private final MyDialog dialog = new MyDialog();

在您的代码中:

private void showDialog() {
   dialog.setVisible(true);
   dialog.requestFocus(); // May be needed to bring window to front
}

这将确保您仅实例化该框一次。每当按下按钮时,只需调用 showDialog() 即可。

Yes, and you don't need to make the box modal to do it (although making it modal would be the easiest way).

Simply do something like the following

In your member delcarations:

private final MyDialog dialog = new MyDialog();

In your code:

private void showDialog() {
   dialog.setVisible(true);
   dialog.requestFocus(); // May be needed to bring window to front
}

This will ensure that you only instantiate the box once. Simply call showDialog() whenever the button is pressed.

埖埖迣鎅 2024-08-26 04:09:33

我过去使用 Swing 所做的另一种方法是,当按下按钮时,我所做的第一件事就是禁用该按钮。然后,我使用可观察模式查看子窗口,并在子窗口关闭时重新启用按钮。这样,如果由于某种原因需要一段时间才能显示子窗口,用户就无法多次单击它并将事情搞砸。

Another way that I've done in the past with Swing is that when the button is pressed the first thing I do is disable the button. Then I use the observable pattern to look at the child window and re-enable the button when the child window is closed. That way if it takes a while to display the child window for some reason the user can't click on it multiple times and mess things up.

罪#恶を代价 2024-08-26 04:09:33

您可以使 JDialog 模式化,然后父窗口在关闭之前不会做出反应。

或者您可以先初始化 JDialog,然后在按下按钮时使其可见。使其可见两次不会将其显示两次。

You could make the JDialog modal, then the parent window would not react until it is closed.

Or you could initialize the JDialog before, and just make it visible when your button is pressed. Making it visible twice will not display it twice.

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