JProgressBar没有进展

所以我试图做一个下载器,用进度条显示下载进度。但是我遇到了问题,因为它实际上没有更新进度条。当它本来是蓝色时,基本上保持白色。如果有人可以帮助,则下面的代码。


JProgressBar progressBar = new JProgressBar(0, ia);

con.add(progressBar, BorderLayout.PAGE_START);

con.validate();

con.repaint();

progressBar = new JProgressBar(0, ia);

progressBar.setValue(0);

System.out.print("Downloading Files");

while ((count = in.read(data, 0, downloadSpeed)) != -1){

    fout.write(data, 0, count);

    if (count >= 2){

        progressBar.setString("Downloading : " + ia + " @ " + count + "Kbs per second");

    } else {

        progressBar.setString("Downloading : " + ia + " @ " + count + "Kb per second");

    }

    progressBar.setValue(count);

    con.add(progressBar, BorderLayout.PAGE_START);

    try{

        Thread.sleep(1000);

    } catch (Exception e){}

}


MM们
浏览 666回答 2
2回答

一只萌萌小番薯

与SwingWorker结合使用。在此处查看示例: SwingWorker和进度栏@气垫船:你是对的。请允许我参考JavaDoc的相应SwingWorker页面,我认为这最好地说明了情况。

牧羊人nacy

正如@happyburnout所指出的那样,最好在单独的线程中处理下载,使用SwingWorker可能是您所做工作的最佳解决方案。主要原因是您阻止了事件调度线程(AKA EDT)的运行,从而阻止了任何重新绘制请求(以及其他UI重要内容)的处理。你应该通读Swing中的并发工人线程和秋千工人现在,这几乎直接从API文档中获取,但是给出了一个SwingWoker带有JProgressBar工人”...public class Worker extends SwingWorker<Object, Object> {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected Object doInBackground() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; // The download code would go here...&nbsp; &nbsp; &nbsp; &nbsp; for (int index = 0; index < 1000; index++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int progress = Math.round(((float)index / 1000f) * 100f);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setProgress(progress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(10);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // You could return the down load file if you wanted...&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }“进度窗格”public class ProgressPane extends JPanel {&nbsp; &nbsp; private JProgressBar progressBar;&nbsp; &nbsp; public ProgressPane() {&nbsp; &nbsp; &nbsp; &nbsp; setLayout(new GridBagLayout());&nbsp; &nbsp; &nbsp; &nbsp; progressBar = new JProgressBar();&nbsp; &nbsp; &nbsp; &nbsp; add(progressBar);&nbsp; &nbsp; }&nbsp; &nbsp; public void doWork() {&nbsp; &nbsp; &nbsp; &nbsp; Worker worker = new Worker();&nbsp; &nbsp; &nbsp; &nbsp; worker.addPropertyChangeListener(new PropertyChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void propertyChange(PropertyChangeEvent evt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ("progress".equals(evt.getPropertyName())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progressBar.setValue((Integer) evt.getNewValue());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; worker.execute();&nbsp; &nbsp; }}记住秋千的黄金法则从不,从不从ThreadEDT中更新任何UI组件始终在其他地方执行耗时的任务 Thread(关于布局管理器的某些东西,更多是个人问题;)使用Swing:D,您将度过轻松愉快的时光
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java