给你个例子,希望对你有帮助。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 <= max) {
simulateTimeConsumingActivity();
SwingUtilities.invokeLater(update);
}
SwingUtilities.invokeLater(finish);
startButton.setEnabled(true);
}
private void updateProgressBar(int value) {
progressBar.setValue(value);
}
private void simulateTimeConsumingActivity() {
try {
Thread.currentThread().sleep(1000);
increment = (max - min) / 10;
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
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 <= max) {
simulateTimeConsumingActivity();
SwingUtilities.invokeLater(update);
}
SwingUtilities.invokeLater(finish);
startButton.setEnabled(true);
}
private void updateProgressBar(int value) {
progressBar.setValue(value);
}
private void simulateTimeConsumingActivity() {
try {
Thread.currentThread().sleep(1000);
increment = (max - min) / 10;
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
解决方案 »
- 急求把网络输入输出流上的数据存为本地二进制文件的方法
- 如何跟踪程序执行的详细信息
- 菜鸟求助!!!!!!!!
- 数据显示
- 请问为什么这个程序打印出来的是乱码?
- 高手看看这个是什么用法?
- JAVA里,怎样将一个字符转换成ASC码..有没有现成的函数
- 今天83%通过JP的感想
- java菜菜鸟提问之二:我想下载个jbuilder,哪儿有,我找到的都没序列号。
- 画线????
- ********************我现在在SOLARIS上安装了ORACLE8I,我想建立数据库,但我有一个问题一直没有解决,请大家进来看看!****************
- 关于java的事件问题,如何在程序中使一个button被click,从而引发这个button的事件处理代码????
其实也就是在使用前调用下面这个方法:
progressBar.setIndeterminate(true);
在jdk1.3就不行,除非自己实现,或用1.4的jprogressbar类。