100,019
社区成员




import java.awt.*;
import javax.swing.*;
public class ThreadTest extends JFrame{
JProgressBar jpb = new JProgressBar(0, 100);
JLabel j1 = new JLabel("正在下载"),j2 = new JLabel();
Thread th = new Thread(new InClass());
Font f = new Font("微软雅黑", Font.PLAIN, 12);
Container c = getContentPane();
public void init() {
jpb.setFont(f);
j1.setFont(f);
j2.setFont(f);
th.start();
c.add(j1,"North");
c.add(j2,"Center");
c.add(jpb,"South");
setVisible(true);
setSize(250,150);
setLocation(150, 100);
setTitle("下载");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
}
class InClass implements Runnable{
int count = 0;
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
jpb.setValue(count);
j2.setText("已完成:" + jpb.getValue() + "%");
if (jpb.getValue() == 100) {
JOptionPane.showMessageDialog(ThreadTest.this, "下载完成","提示", JOptionPane.QUESTION_MESSAGE);
System.exit(0);
}
c.add(j1,"North");
c.add(j2,"Center");
c.add(jpb,"South");
c.validate();
}
}
public static void main(String[] args) {
new ThreadTest().init();
}
}
进度条无反应,不知道哪里出了异常
不要在子线程里面进行界面修改,界面修改应该使用Message 对象发到主线程去修改