3

我需要一个关于在 Java 中开发 GUI 的进度条的非常简单的(骨架)教程。进度条可以是“不确定的”,最好有一个可更新的文本标签。

这是一些骨架代码,我希望有人可以对其进行扩展以使其正常工作。

class MyFunView extends FrameView {

  // class vars and other stuff...

  @Action
  public void excitingButtonPressed() {

     /* I have n files to download */
     // Download file 1, set progress bar status to "downloading file 1"
     // ...
     // Download file n, set progress bar status to "downloading file 2"

  }

} 

基本上,我如何以及如何包装我的下载代码?(我知道需要某种 Task 的子类?)

提前非常感谢。

编辑:

我们都只有这么多宝贵的时间来活着,而且没有任何方法可以像我希望的那样用尽可能少的代码或时间来完成,所以我不会使用进度条。

4

2 回答 2

6

我需要一个关于在 Java 中开发 GUI 的进度条的非常简单的(骨架)教程。

于 2012-05-02T22:19:23.057 回答
3

You can use SwingWorker in conjunction with a JProgressBar.

The other alternative is to execute your task (e.g. Runnable) and periodically update the JProgressBar. Beware though, that in Swing any UI manipulations have to be done on the EDT. SwingWorker has a nice property of taking care of this detail. Your own solution will have to use SwingUtilities.invokeLater,. for example:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        progressBar.setValue(progressCounter++);
    }
});

Lot of code for a stupid thing, isn't it? Maybe the SwingWorker with a PropertyChangeListener is a better solution in the end...

于 2012-05-02T22:19:26.753 回答