关闭

有关进度条 JProgressBar的应用

59人阅读 评论(0) 收藏 举报
package 线程应用;
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;
import javax.swing.JTextField;
public class SwingThreadTest2 extends JFrame {
private static final long serialVersionUID = 1L;
private static final String STR = "Completed : ";
private JProgressBar progressBar = new JProgressBar();
private JTextField text = new JTextField(10);
private JButton start = new JButton("Start");
private JButton end = new JButton("End");
private boolean flag = false;
private int count = 0;

GoThread t = null;
public SwingThreadTest2() {
this.setLayout(new FlowLayout());
add(progressBar);
text.setEditable(false);
add(text);
add(start);
add(end);
start.addActionListener(new Start());
end.addActionListener(new End());
}
private void go() {
while (count < 100) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (flag) {
count++;
System.out.println(count);
progressBar.setValue(count);
text.setText(STR + String.valueOf(count) + "%");
progressBar.setStringPainted(true);
progressBar.setString(String.valueOf(count) + "%");
}
}
}
private class Start implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = true;
if(t == null){
t = new GoThread();
t.start();
}
}
}
//执行复杂工作,然后更新组件的线程
class GoThread extends Thread{
public void run() {
//do something...
go();
}
}
private class End implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = false;
}
}
public static void main(String[] args) {
SwingThreadTest2 fg = new SwingThreadTest2();
fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fg.setSize(300, 100);
fg.setVisible(true);
}
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1489次
    • 积分:52
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档