使用invokeLater一一显示对话框

发布于 2024-10-04 17:07:02 字数 564 浏览 6 评论 0原文

在 2 个不同的动作监听器中,当满足某些条件时将显示一个对话框。 如果两个动作监听器都需要显示对话框,则将同时显示 2 个对话框。但我希望将它们一一展示出来。

简化的代码:

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(getTopLevelAncestor(), "dialog 1");
            }
        });

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(getTopLevelAncestor(), "dialog 2");
            }
        });

这 2 个“SwingUtilities.invokeLater”调用位于不同的类中。

In 2 different action listeners, a dialog will be shown when some conditions are met.
If both of the action listeners need to show dialog, 2 dialogs will be shown at the same time. But I want them to be shown one by one.

Simplified code:

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(getTopLevelAncestor(), "dialog 1");
            }
        });

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(getTopLevelAncestor(), "dialog 2");
            }
        });

Those 2 "SwingUtilities.invokeLater" invocations are in different classes.

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

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

发布评论

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

评论(3

债姬 2024-10-11 17:07:03

您需要 < code>invokeAndWait(),此方法等待直到 Runnable 完成。

或者在您的情况下,当第一个对话框关闭时。

You need invokeAndWait(), this method waits until the Runnable has been finished.

Or in your situation, when the first dialog has been closed.

度的依靠╰つ 2024-10-11 17:07:02

创建一个类来跟踪这一点;此类将包含要显示的对话框队列;每当关闭对话框时,都会显示队列中的第一个对话框并从队列中删除。当另一个类需要显示对话框时,如果队列为空,则立即显示该对话框,否则将其插入队列中。

Make a class that keeps track about that; this class would contain a queue of dialogs to display; whenever a dialog is closed, the first one of the queue is shown and removed from the queue. When another class needs to show a dialog, it's immediately shown if the queue is empty, or inserted into the queue else.

十秒萌定你 2024-10-11 17:07:02

这与对话的形式有关。关于这个主题有一篇非常有用的文章 http://java.sun。 com/developer/technicalArticles/J2SE/Desktop/javase6/modality/。对话具有不同的模态类型和不同的优先级。您可以通过创建优先级较低的第二个对话框来解决您的问题:

JOptionPane pane = new JOptionPane("dialog 2", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog("Message");
dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
dialog.setVisible(true);

希望这会有所帮助。

This is related to the modality of dialogs. There is quite useful article about this topic http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/modality/. Dialogs have different modality types with different priorities. You can solve your problem by creating the second dialog with lower priority:

JOptionPane pane = new JOptionPane("dialog 2", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog("Message");
dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
dialog.setVisible(true);

Hopefully this helps.

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