如何添加进度条? [英] How to add a progress bar?

查看:115
本文介绍了如何添加进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力了解如何添加进度条,我可以在我正在实现的GUI中创建一个进度条并让它出现,但即使在检查完 http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html 我仍然不清楚如何将方法设置为任务,以便我可以创建运行方法的进度条。请有人尝试向我解释这个问题,或者发布一个在GUI中使用的进度条的示例,其中任务被设置为方法。谢谢。

I have been trying to understand how to add a progress bar, I can create one within the GUI I am implementing and get it to appear but even after checking through http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html I am still no clearer on how I can set a method as a task so that I can create a progress bar for running a method. Please can someone try to explain this to me or post an example of a progress bar being used in the GUI with a task being set as a method. Thanks.

推荐答案

也许我可以用一些示例代码来帮助你:

Maybe i can help you with some example code:

public class SwingProgressBarExample extends JPanel {

  JProgressBar pbar;

  static final int MY_MINIMUM = 0;

  static final int MY_MAXIMUM = 100;

  public SwingProgressBarExample() {
    // initialize Progress Bar
    pbar = new JProgressBar();
    pbar.setMinimum(MY_MINIMUM);
    pbar.setMaximum(MY_MAXIMUM);
    // add to JPanel
    add(pbar);
  }

  public void updateBar(int newValue) {
    pbar.setValue(newValue);
  }

  public static void main(String args[]) {

    final SwingProgressBarExample it = new SwingProgressBarExample();

    JFrame frame = new JFrame("Progress Bar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(it);
    frame.pack();
    frame.setVisible(true);

    // run a loop to demonstrate raising
    for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
      final int percent = i;
      try {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            it.updateBar(percent);
          }
        });
        java.lang.Thread.sleep(100);
      } catch (InterruptedException e) {
        ;
      }
    }
  }
}

这篇关于如何添加进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆