Java Swing GUI 沙漏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1940966/
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
Java Swing GUI hour glass
提问by Frank
There is a JTabbedPaneIn my Swingprogram.
When user clicks on a tab, the program takes a while to get the data and process the results, then shows the results in the selected tab.
有一个JTabbedPaneIn my Swing程序。当用户单击选项卡时,程序需要一段时间来获取数据并处理结果,然后在所选选项卡中显示结果。
How can I display a hour glass, or something of that effectso that user knows it's processing data? Not to click on the tab again before it finishes it job.
我如何显示沙漏或类似效果的东西,以便用户知道它正在处理数据?在完成工作之前不要再次单击选项卡。
回答by Dan Dyer
The simplest way is to just call setCursoron the appropriate component (probably the top-level window) with the appropriate Cursor.
最简单的方法是使用适当的Cursor在适当的组件(可能是顶级窗口)上调用setCursor。
component.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
And then set it back when you are done.
然后在完成后将其放回原处。
component.setCursor(Cursor.getDefaultCursor());
回答by Michael Borgwardt
A JProgressBar(possibly in indetermiante mode) sounds right - put that on the tab until the data has been fetched. A well-designed UI shouldn't force the user to wait for long-running tasks to complete and instead allow them to do something else inbetween.
一个不确定JProgressBar(可能在indetermiante模式)听上去不错-把该选项卡上,直到数据被取出。设计良好的 UI 不应该强迫用户等待长时间运行的任务完成,而是允许他们在中间做其他事情。
回答by Hermann Hans
setCursor(int) is deprecated. This is probably a bit cleaner:
不推荐使用 setCursor(int)。这可能更干净一点:
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
回答by clartaq
As the other answers mention, you can set a wait cursor, but you also mention preventing additional mouse clicks. You can use a glass pane to prevent clicks on components until the long operation is finished. In addition to the Sun tutorialson the glass pane, there is a nice example at http://www.java2s.com/Code/Java/Swing-JFC/DemonstrateuseofGlassPane.htm
正如其他答案所提到的,您可以设置一个等待光标,但您也提到了防止额外的鼠标点击。您可以使用玻璃窗格来防止在长时间操作完成之前点击组件。除了玻璃面板上的Sun 教程之外,http://www.java2s.com/Code/Java/Swing-JFC/DemonstrateuseofGlassPane.htm 上还有一个很好的示例
回答by John Doe
I would as other mentioned
我会像其他人提到的那样
- Change the cursor
- Use a SwingWorker
- Display the progressbar or an animated image in the glasspane
- Hide the glasspane when the task is completed
- Restore the default cursor
- 改变光标
- 使用 SwingWorker
- 在玻璃面板中显示进度条或动画图像
- 任务完成后隐藏玻璃板
- 恢复默认光标

