JProgressBar没有进展 [英] JProgressBar isn't progressing

查看:74
本文介绍了JProgressBar没有进展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试使用进度条创建一个显示下载进度的下载程序。
但我遇到了问题,因为它实际上并没有更新进度条。基本上它保持白色,当它意味着蓝色。如果有人可以帮忙,代码就在下面。

So i'm trying to make a downloader which shows the progress of the download with a progress bar. But i'm having problems since it doesn't actually update the progress bar. Basically it stays white, when it is meant to be blue. If anyone could help, code is below.

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){}
}


推荐答案

与SwingWorker结合使用。
在此处查看示例:
SwingWorker和Progress Bar

Use a combination with SwingWorker. See an example here: SwingWorker and Progress Bar

@Hovercraft:你是对的。请允许我参考相应的 JavaDoc的SwingWorker页面,在我看来,这最能说明情况。

@Hovercraft: You're right. Allow me to refer to the corresponding SwingWorker page of JavaDoc, in my opinion this explains the situation best.

这篇关于JProgressBar没有进展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆