创建进度条以使用java中的for循环的结果进行更新

时间:2017-11-20 14:02:17

标签: java swing for-loop jprogressbar

我创建了一个从0到850000的for循环。我选择了一个高数字,因为我想创建一个进度条,它将使用for循环中的结果进行更新。这是我到目前为止的代码。

public class ProgressBarTest extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {





        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ProgressBarTest frame = new ProgressBarTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ProgressBarTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        for(int i = 850000; i>0; i--) {
            System.out.println("i = " + i);
        }

    }

}

我似乎找不到使用for循环来更新进度条的方法。我确实创建了一个可以在计时器上工作的进度条,但这不是我想要它做的。我基本上希望它从0%更新到100%。这对我来说似乎很简单,但我已经坚持了一段时间。我感谢任何类型的示例代码,以帮助我理解它是如何工作的。

2 个答案:

答案 0 :(得分:3)

以下是使用Saran

提出的SwingWorker的完整示例
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;

public class ProgressBarTest extends JFrame {

    private static final long LOOP_LENGTH = 85000000;

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    ProgressBarTest frame = new ProgressBarTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ProgressBarTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        JProgressBar progress = new JProgressBar();
        progress.setStringPainted(true);
        contentPane.add(new JLabel("Loop progress is: "), BorderLayout.NORTH);
        contentPane.add(progress, BorderLayout.SOUTH);
        setContentPane(contentPane);
        ProgressWorker worker = new ProgressWorker(progress);
        worker.execute();
    }

    private static class ProgressWorker extends SwingWorker<Void, Integer> {
        private final JProgressBar progress;

        public ProgressWorker(JProgressBar progress) {
            this.progress = progress;
        }

        @Override
        protected Void doInBackground() throws Exception {
            for (long i = LOOP_LENGTH; i > 0; i--) {
                final int progr = (int) ((100L * (LOOP_LENGTH - i)) / LOOP_LENGTH);
                publish(progr);
            }
            return null;
        }

        @Override
        protected void process(List<Integer> chunks) {
            progress.setValue(chunks.get(chunks.size() - 1));
            super.process(chunks);
        }

        @Override
        protected void done() {
            progress.setValue(100);
        }
    }
}

答案 1 :(得分:2)

您需要使用SwingWorker,当Swing程序需要执行长时间运行的任务时,它通常使用其中一个工作线程。所以,试试这个:

$query = mysqli_query($connect,$Sql);

在你的类中,通过调用我上面写的类的新类替换循环,并传递一个新的JProgressBar()并添加.execute(),如下所示:

public class JProgressBarTest extends SwingWorket<Void,Integer>{

private JProgressBar jpb;
  public ProgrssBarTest(JProgressBar jpb){
    this.jpb = jpb;
  }

  @Override
  protected void process(List<Integer> chunks){
    int i = chunks.get(chunks.size()-1);
    jpb.setValue(i);
  }

  @Override
  protected void doInBackground() throws Exception{
    int x = 0;
    for(int i = 850000; i>0; i--) {
        publish(x); //replace x with the number you want to increment the progressbar each time pubish is called
        x = x + i; //or replace i with the increment you want
        System.out.println("i = " + i);
    }
  }

  @Override
  protected void done(){
    try{
      get();
    }catch(ExecutionException |InterruptedException e){
      ;
    }
  }

}