package com.wet.thread;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* 进度条的使用(多线程)
*
* @author Administrator
*
*/
public class TestProgressBarThread extends JFrame implements Runnable {
private JProgressBar progressBar;
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestProgressBarThread frame = new TestProgressBarThread();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public TestProgressBarThread() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
progressBar = new JProgressBar();
progressBar.setBounds(10, 106, 457, 45);
getContentPane().add(progressBar);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
/*
* for (int i = 0; i <= 100; i++) { progressBar.setValue(i); try
* { Thread.currentThread().sleep(100); } catch
* (InterruptedException e1) { e1.printStackTrace(); } }
*/
//启动线程
new Thread(new TestProgressBarThread()).start();
}
});
button.setText("开始");
button.setBounds(159, 230, 106, 28);
getContentPane().add(button);
}
public void run() {
for (int i = 0; i <= 100; i++) {
progressBar.setValue(i);
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
405

被折叠的 条评论
为什么被折叠?



