java Swing 中的进度对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/511857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Progress Dialog in Swing
提问by Allain Lalonde
How can I make a modal JDialogwithout buttons appear for the duration it takes a Runnableinstance to complete and have that instance update a progress bar/message on that dialog?
如何JDialog在Runnable实例完成期间显示没有按钮的模态并让该实例更新该对话框上的进度条/消息?
Clearly spaghetti code might work, but I'm looking for a clean design if one exists.
显然,意大利面条式代码可能有效,但我正在寻找一种干净的设计(如果存在的话)。
回答by Michael Myers
You might want to look into ProgressMonitor. It automatically pops up a dialog with a progress bar if the operation is taking a long time; see How to Use Progress Monitors.
您可能想查看ProgressMonitor。如果操作时间长,它会自动弹出一个带有进度条的对话框;请参阅如何使用进度监视器。
回答by carlo.milanesi
Look at the project described at the following URL: http://digilander.libero.it/carlmila/compmon/main.html
查看以下 URL 中描述的项目:http: //digilander.libero.it/carlmila/compmon/main.html
It is a free Java library alternative to the Swing ProgressMonitor.
它是 Swing ProgressMonitor 的免费 Java 库替代品。
回答by Aaron Digulla
Use a monitor class whit a global instance and which your code keeps up to date (I'm starting, I'm working, I'm at xxx%, I'm done).
使用带有全局实例的监视器类并且您的代码保持最新(我正在开始,我正在工作,我在 xxx%,我已经完成了)。
The monitor can then decide to show the dialog and keep it current. Later, you can simply replace the dialog with a progress bar in the status bar, for example. Use an interface for the monitor (methods: start(), update(), end(), error(), isAborted()) so you can replace it with something else, too.
然后监视器可以决定显示对话框并使其保持最新状态。例如,稍后您可以简单地将对话框替换为状态栏中的进度条。使用监视器的接口(方法:start()、update()、end()、error()、isAborted()),这样您也可以用其他东西替换它。
You can extend the monitor to wait for 500ms after start() if there is an end() and not show the dialog in this case, etc.
如果有 end() 并且在这种情况下不显示对话框等,您可以扩展监视器以在 start() 之后等待 500 毫秒。
This is how Eclipsedoes it and it works well.
这就是Eclipse 的工作方式,并且运行良好。

