执行后台任务时不确定JProgressBar

时间:2017-01-12 10:29:44

标签: java multithreading swing swingworker jprogressbar

我想在前台显示JProgressBar并在后台运行任务,但我的程序不能简单地启动进度条。

import java.awt.EventQueue;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JProgressBar;


public class sw {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                sw window = new sw();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 * @throws InterruptedException 
 */
public sw() throws InterruptedException {
    initialize();
}

/**
 * Initialize the contents of the frame.
 * @throws InterruptedException 
 */
private void initialize() throws InterruptedException {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setIndeterminate(true);
    progressBar.setBounds(96, 91, 146, 14);
    frame.getContentPane().add(progressBar);
    progressBar.setVisible(true);
    for(int i=0;i<5;i++)
    {
        System.out.println(i);
        TimeUnit.SECONDS.sleep(1);
    }
    progressBar.setVisible(false);
   }
   }

如何去做?

0 个答案:

没有答案