线程小例子--进度条

线程小例子--进度条

功能:在窗体中显示一个进度条,该进度条自动递增

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class ThreadDemo extends JFrame{
    Thread thread;
    public  ThreadDemo(){
        super();
        Container c=getContentPane();
        setBounds(300,200,250,100);
        final JProgressBar b=new JProgressBar();  //进度条
        c.add(b);
        b.setStringPainted(true); //设置进度条上显示数字
        thread=new Thread(new Runnable() {
            int count=0;
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while(true){
                b.setValue(count++);  //设置进度条当前值
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
            }
        });
        thread.start();
        setVisible(true);
    }
    public static void main(String[] args){
        new ThreadDemo();
    }
}

唯一需要的大概就是进度条的知识点吧:

JProgressBar:

setStringPainted 设置进度条上是否显示数字
setValue  设置进度条的当前值

结果如下:

 

 
posted @ 2018-03-20 17:21  xtu熊大  阅读(198)  评论(0编辑  收藏  举报