0
嗨。我正准备一个java表单,我想在每个0.5秒的时间间隔内显示一个值为1到5的进度条,但在我的代码中,进度条在2.5秒后直接从1跳到5 .. 请帮助我的代码...jprogressbar在秋千
public class cafee extends JFrame implements ActionListener {
JProgressBar pr1;
JButton b1;
public cafee() {
setLayout(new FlowLayout());
pr1 = new JProgressBar();
pr1.setSize(10, 1);
pr1.setForeground(Color.blue);
pr1.setMinimum(0);
pr1.setMaximum(5);
pr1.setValue(0);
pr1.setVisible(true);
b1 = new JButton();
b1.setVisible(true);
add(pr1);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
for (int a = 1; a <= 5; a++) {
try {
pr1.setValue(a);
Thread.sleep(500);
} catch (Exception e) {
}
}
}
}
public static void main(String[] args) {
cafee caf = new cafee();
caf.setVisible(true);
caf.setSize(500, 500);
}
}
事实上,SwingWorker是要走的路。在EDT(Event Dispatch Thread)和SwingWorker上阅读,尤其是它的publish()方法可以让你从后台线程与EDT进行“交谈”。 – sbrattla 2011-12-17 15:55:19