当前位置: 技术问答>java相关
[求助]一个不知道执行长度的任务怎么用JProgressBar来表示呀
来源: 互联网 发布时间:2015-05-04
本文导语: 就是说我有一段代码无法知道需要执行多长时间,在这段代码执行的同时我想用一个JProgressBar进行表示,请问如何实现。下面是API上的一段话,可我没太明白。 Here is an example of putting a progress bar into indetermina...
就是说我有一段代码无法知道需要执行多长时间,在这段代码执行的同时我想用一个JProgressBar进行表示,请问如何实现。下面是API上的一段话,可我没太明白。
Here is an example of putting a progress bar into indeterminate mode, and then switching back to determinate mode once the length of the task is known:
progressBar = new JProgressBar();
...//when the task of (initially) unknown length begins:
progressBar.setIndeterminate(true);
...//do some work; get length of task...
progressBar.setMaximum(newLength); progressBar.setvalue(newvalue); progressBar.setIndeterminate(false);
Here is an example of putting a progress bar into indeterminate mode, and then switching back to determinate mode once the length of the task is known:
progressBar = new JProgressBar();
...//when the task of (initially) unknown length begins:
progressBar.setIndeterminate(true);
...//do some work; get length of task...
progressBar.setMaximum(newLength); progressBar.setvalue(newvalue); progressBar.setIndeterminate(false);
|
给你个例子,希望对你有帮助。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
private JProgressBar progressBar = new JProgressBar();
private JButton startButton = new JButton("start");
public void init() {
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(startButton);
contentPane.add(progressBar);
progressBar.setStringPainted(true);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
(new UpdateThread()).start();
}
});
}
class UpdateThread extends Thread {
Runnable update, finish;
int value, min, max, increment;
public UpdateThread() {
max = progressBar.getMaximum();
min = progressBar.getMinimum();
update = new Runnable() {
public void run() {
value = progressBar.getValue() + increment;
updateProgressBar(value);
}
};
finish = new Runnable() {
public void run() {
updateProgressBar(min);
}
};
}
public void run() {
startButton.setEnabled(false);
while(value + increment
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
private JProgressBar progressBar = new JProgressBar();
private JButton startButton = new JButton("start");
public void init() {
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(startButton);
contentPane.add(progressBar);
progressBar.setStringPainted(true);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
(new UpdateThread()).start();
}
});
}
class UpdateThread extends Thread {
Runnable update, finish;
int value, min, max, increment;
public UpdateThread() {
max = progressBar.getMaximum();
min = progressBar.getMinimum();
update = new Runnable() {
public void run() {
value = progressBar.getValue() + increment;
updateProgressBar(value);
}
};
finish = new Runnable() {
public void run() {
updateProgressBar(min);
}
};
}
public void run() {
startButton.setEnabled(false);
while(value + increment