使用invokeLater一一显示对话框
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要 < 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.
创建一个类来跟踪这一点;此类将包含要显示的对话框队列;每当关闭对话框时,都会显示队列中的第一个对话框并从队列中删除。当另一个类需要显示对话框时,如果队列为空,则立即显示该对话框,否则将其插入队列中。
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.
这与对话的形式有关。关于这个主题有一篇非常有用的文章 http://java.sun。 com/developer/technicalArticles/J2SE/Desktop/javase6/modality/。对话具有不同的模态类型和不同的优先级。您可以通过创建优先级较低的第二个对话框来解决您的问题:
希望这会有所帮助。
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:
Hopefully this helps.