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

查看:28
本文介绍了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 和进度条

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天全站免登陆