JProgressBar Demo 线程实时刷新信息

http://www.cnblogs.com/leajon-first/archive/2012/10/26/2740633.html


涉及三个线程:
主线程:主线程运行程序,
任务线程:然后打开一个新线程来处理任务,
刷新线程:在处理任务的时候,需要刷新主线程里面的信息,
那么应该重新开第三个线程来刷新主线程的信息。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class PgbDemo extends JFrame {

    private JProgressBar pgb;
    private JButton but;
    private JPanel pan;

    public PgbDemo() {
        super("JProgressBar Demo");
        initComponents();
    }

    private void initComponents() {
        pan = new JPanel();
        pan.setLayout(new BorderLayout());
        pgb = new JProgressBar();
        pgb.setMinimum(0);
        pgb.setMaximum(100);
        pgb.setValue(0);
        pgb.setStringPainted(true);
        pgb.setBorderPainted(true);
        pgb.setBackground(Color.yellow);
        pan.add(pgb, BorderLayout.CENTER);

        but = new JButton("Start");
        but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //run task
                but.setEnabled(false);
                new ATask().start();
            }
        });
        pan.add(but, BorderLayout.EAST);

        this.getContentPane().add(pan);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                PgbDemo frame = new PgbDemo();
                frame.setLocationRelativeTo(null);
                frame.setSize(500, 100);
                frame.setVisible(true);
            }
        });
    }

    class ATask extends Thread {
        public void run() {
            System.out.println(SwingUtilities.isEventDispatchThread());
            for (int i = 0; i < 130; i++) {
                final int percents = (int) ((float) i / 130.0 * 100.0) + 1;
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        PgbDemo.this.pgb.setValue(percents);
                    }
                });
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(PgbDemo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    PgbDemo.this.but.setEnabled(true);
                }
            });
        }
    }
}


多个线程,比如一个线程做业务,一个线程专门处理进度条
package tools;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

/**
 * 涉及到四个个线程
 * 1.主线程
 * 2.进度条线程
 * 3.做业务的线程
 * 4.改变进度条值的线程(不断创建,消失)
 * @author Administrator
 *
 */
public class PgbDemo extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JProgressBar pgb;
	private JButton but;
	private JPanel pan;

	private  int max = 100;
	private int current = 0;

	public PgbDemo() {
		super("JProgressBar Demo");
		initComponents();
	}

	private void initComponents() {
		pan = new JPanel();
		pan.setLayout(new BorderLayout());
		pgb = new JProgressBar();
		pgb.setMinimum(0);
		pgb.setMaximum(100);
		pgb.setValue(0);
		pgb.setStringPainted(true);
		pgb.setBorderPainted(true);
		pgb.setBackground(Color.yellow);
		pan.add(pgb, BorderLayout.CENTER);

		but = new JButton("Start");
		but.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// run task
				but.setEnabled(false);
				new ATask().start();
				new ATask1().start();
				System.out.println("AAAAAAA");
			}
		});
		pan.add(but, BorderLayout.EAST);

		this.getContentPane().add(pan);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				PgbDemo frame = new PgbDemo();
				frame.setLocationRelativeTo(null);
				frame.setSize(500, 100);
				frame.setVisible(true);
			}
		});
	}

	class ATask1 extends Thread {
		public void run() {
			System.out.println(SwingUtilities.isEventDispatchThread());
			while (current <= max) {
				System.out.println(current);
				try {
					Thread.sleep(30);
				} catch (InterruptedException ex) {
					Logger.getLogger(PgbDemo.class.getName()).log(Level.SEVERE, null, ex);
				}
				//同步操作
				synchronized(this){
					current++;
				}
				
			}
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					PgbDemo.this.but.setEnabled(true);
				}
			});
		}
	}

	class ATask extends Thread {
		public void run() {
			System.out.println(SwingUtilities.isEventDispatchThread());
			int currPercents = 0;//用来跳出判断
			while(currPercents<=100){
				System.out.println("----------------current=>" + current);
				System.out.println("----------------max=>" + max);
				final int  percents = (int) ((float) current / max * 100.0) + 1;
				currPercents= percents;
				System.out.println("----------------percents=>" + percents);
				SwingUtilities.invokeLater(new Runnable() {
					//设定进度条的值,是需要在一个线程里面的
					public void run() {
						PgbDemo.this.pgb.setValue(percents);
					}
				});
				try {
					Thread.sleep(50);
				} catch (InterruptedException ex) {
					Logger.getLogger(PgbDemo.class.getName()).log(Level.SEVERE, null, ex);
				}
			}
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					PgbDemo.this.but.setEnabled(true);
				}
			});
		}
	}
}

你可能感兴趣的:(ProgressBar)