Swing 进度条使用

时间:2023-01-28 12:01:34

jframe

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ProgINDemo extends JFrame {
public static void main(String arg[]) {
new ProgINDemo();
}
public ProgINDemo() {
int height;
int width;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); } } );

setLocation(200,150);
Container pane = getContentPane();
pane.setLayout(null);
Insets insets = pane.getInsets();

JProgressBar bar = new JProgressBar(0,100);
bar.setIndeterminate(true);
bar.setValue(0);//设置进度条的值
bar.setStringPainted(true);//设置在进度条中显示百分比

Dimension dim = bar.getPreferredSize();
int x = insets.left + 20;
int y = insets.top + 20;
dim.width += 100;
bar.setBounds(x,y,dim.width,dim.height);
pane.add(bar);

width = x + dim.width + 20 + insets.left;
height = y + dim.height + 40 + insets.bottom;

ProgINDemoTask task = new ProgINDemoTask(bar);
task.start();//进度条开始运行

setSize(width,height);
setVisible(true);
}
}
JProgressBar 作为Thread处理线程中的构造函数参数传入,可以改变进度条
class ProgINDemoTask implements Runnable {//实现Runnable,创建一个线程private boolean running;private Thread looper;private JProgressBar bar;private int value;ProgINDemoTask(JProgressBar bar) {this.bar = bar;running = false;}public void start() {//线程启动方法if(!running) {value = 0;bar.setValue(value);running = true;looper = new Thread(this);looper.start();}}public void run() {//线程运行中的动作try {while(running) {Thread.sleep(200);bar.setValue(++value);if(value >= 100)//到100停下running = false;}} catch(Exception e) {System.out.println(e.getStackTrace());}}}
Thread处理线程中更新了progressBar的值,在frame里不会跟着改变,需要重绘调用bar.updateUI()方法