java swing 确定进度条

package toolbar;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JProgressbar extends JFrame {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JProgressbar probar = new JProgressbar();
		probar.setVisible(true);
	}
	public JProgressbar() {
		super();
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		setTitle("进度条");
		setBounds(100, 100, 500, 375);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		final JLabel label = new JLabel();
		label.setForeground(new Color(255,0,0));
		label.setText("welcome to use update automaticlly");

		c.add(label, new FlowLayout());
		final JProgressBar progressBar = new JProgressBar();
/*		progressBar.setStringPainted(true);// 设置显示提示信息
		progressBar.setIndeterminate(true);// 设置采用不确定进度条
		progressBar.setString("升级进行中......");// 设置提示信息
*/
		getContentPane().add(progressBar, new FlowLayout());
		
		final JButton button = new JButton();
		button.setText("完成");
		button.setEnabled(false);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		

		getContentPane().add(button, new FlowLayout());
		Progress pro = new Progress(progressBar, button);// 利用线程模拟一个在线升级任务
		Thread t1 = new Thread(pro,"线程1");
		t1.start();
		
	}
	class Progress implements Runnable {
		private final int[] progressValue = { 6, 18, 27, 39, 51, 66, 81, 100 };
		private JProgressBar progressbar;
		private JButton jbutton;

		public Progress(JProgressBar progressbar, JButton jbutton) {
			this.progressbar=progressbar;
			this.jbutton=jbutton;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			for (int i = 0; i < progressValue.length; i++) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(progressValue[i]); 
				progressbar.setValue(progressValue[i]);
				System.out.println("2");
			}
			progressbar.setIndeterminate(false);
			progressbar.setString("升级完成");
			jbutton.setEnabled(true);
		}

	}
}



你可能感兴趣的:(java swing 确定进度条)