1.调用
public void runProgressBar(int time, String message, String ip, String mac) {
progressBar.setVisible(true);
new Thread(new RefrushMainThread(time, message, ip, mac, progressBar, jLabel11, this)).start();
}
2.示例
/**
* 刷新进度条
* @author liujiao
*/
class RefreshThread extends Thread {
// Runnable update, finish;
int value, max;
String ip, message, method;
SlaverListPanel panel;
JComponent jComponent;
javax.swing.JProgressBar progressBar;
JLabel progressLabel;
public RefreshThread(String ip, final String message, String method, SlaverListPanel panel, JComponent jComponent, javax.swing.JProgressBar progressBar, JLabel jLabel11) {
max = progressBar.getMaximum();
// min = progressBar.getMinimum();
this.ip = ip;
this.message = message;
this.method = method;
this.panel = panel;
this.jComponent = jComponent;
this.progressBar = progressBar;
this.progressLabel = jLabel11;
}
@Override
public void run() {
ConfigBat configbat = new ConfigBat();
ConfigBatThread c1 = null;
ConfigBatThread c2 = null;
ConfigThread c3 = null;
progressBar.setVisible(true);
String localip = CurrentUser.getInstance().getIp();
if (method.equals("get_config.bat")) {
c1 = new ConfigBatThread(localip, ip, "", method);
c3 = new ConfigThread(ip, panel);
progressLabel.setVisible(true);
c1.start();
while (value <= max) {
try {
Thread.sleep(60);
} catch (InterruptedException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
value++;
if (value == 70) {
while (c1.isAlive()) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
c3.start();
// c3.start();
}//if:c1线程结束
if (value == 80) {
while (c3.isAlive()) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
progressBar.setValue(value);
MainProgressBar.initIcon(value, progressLabel);
}
}
JOptionPane.showMessageDialog(null, "刷新成功!");
progressBar.setValue(0);
progressBar.setVisible(false);
progressLabel.setVisible(false);
jComponent.setEnabled(true);
}
}
3.辅助
public class MainProgressBar {
public static void initIcon(int value, JLabel progressLabel) {
if (value <= 100) {
progressLabel.setText(" 目前已完成:" + value + "%");
}
}
}
public void runProgressBar(int time, String message, String ip, String mac) {
progressBar.setVisible(true);
new Thread(new RefrushMainThread(time, message, ip, mac, progressBar, jLabel11, this)).start();
}
2.示例
/**
* 刷新进度条
* @author liujiao
*/
class RefreshThread extends Thread {
// Runnable update, finish;
int value, max;
String ip, message, method;
SlaverListPanel panel;
JComponent jComponent;
javax.swing.JProgressBar progressBar;
JLabel progressLabel;
public RefreshThread(String ip, final String message, String method, SlaverListPanel panel, JComponent jComponent, javax.swing.JProgressBar progressBar, JLabel jLabel11) {
max = progressBar.getMaximum();
// min = progressBar.getMinimum();
this.ip = ip;
this.message = message;
this.method = method;
this.panel = panel;
this.jComponent = jComponent;
this.progressBar = progressBar;
this.progressLabel = jLabel11;
}
@Override
public void run() {
ConfigBat configbat = new ConfigBat();
ConfigBatThread c1 = null;
ConfigBatThread c2 = null;
ConfigThread c3 = null;
progressBar.setVisible(true);
String localip = CurrentUser.getInstance().getIp();
if (method.equals("get_config.bat")) {
c1 = new ConfigBatThread(localip, ip, "", method);
c3 = new ConfigThread(ip, panel);
progressLabel.setVisible(true);
c1.start();
while (value <= max) {
try {
Thread.sleep(60);
} catch (InterruptedException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
value++;
if (value == 70) {
while (c1.isAlive()) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
c3.start();
// c3.start();
}//if:c1线程结束
if (value == 80) {
while (c3.isAlive()) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
progressBar.setValue(value);
MainProgressBar.initIcon(value, progressLabel);
}
}
JOptionPane.showMessageDialog(null, "刷新成功!");
progressBar.setValue(0);
progressBar.setVisible(false);
progressLabel.setVisible(false);
jComponent.setEnabled(true);
}
}
3.辅助
public class MainProgressBar {
public static void initIcon(int value, JLabel progressLabel) {
if (value <= 100) {
progressLabel.setText(" 目前已完成:" + value + "%");
}
}
}