JProgressBar Demo 執行緒實時重新整理資訊

不淨之心發表於2013-03-17
[url]http://www.cnblogs.com/leajon-first/archive/2012/10/26/2740633.html[/url]


涉及三個執行緒:
主執行緒:主執行緒執行程式,
任務執行緒:然後開啟一個新執行緒來處理任務,
重新整理執行緒:在處理任務的時候,需要重新整理主執行緒裡面的資訊,
那麼應該重新開第三個執行緒來重新整理主執行緒的資訊。

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);
}
});
}
}
}

相關文章