Java 如何使用进度条从网站下载应用程序?

Java 如何使用进度条从网站下载应用程序?,java,swing,event-dispatch-thread,jprogressbar,Java,Swing,Event Dispatch Thread,Jprogressbar,我在应用程序中使用进度条,从webaite下载内容并将其保存到数据库中。我放了一个进度条来指示下载是否完成,但问题是当所有内容都保存在我的数据库中时,进度条会变成100%。下载时,它应该像5%,10%,25%等,然后在所有保存后,它将成为100%。我该怎么做?我编写的示例代码 timer = new Timer(interval, new ActionListener() { public void actionPerformed(ActionEvent evt) { if (j ==100)

我在应用程序中使用进度条,从webaite下载内容并将其保存到数据库中。我放了一个进度条来指示下载是否完成,但问题是当所有内容都保存在我的数据库中时,进度条会变成100%。下载时,它应该像5%,10%,25%等,然后在所有保存后,它将成为100%。我该怎么做?我编写的示例代码

timer = new Timer(interval, new ActionListener() {

public void actionPerformed(ActionEvent evt) {
if (j ==100){
//a beep is heard when download is completed
Toolkit.getDefaultToolkit().beep();
timer.stop();
jButton3.setEnabled(true);
pb.setValue(0);
String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + "Downloading completed." + "</b>" + "</font>" + "</html>";
lbldownload.setText(str);
            }
j = j + 1;
pb.setValue(j);
        }
});

jButton3.setEnabled(false);
j = 0;
String str = "<html>" + "<font color=\"#008000\">" + "<b>" + "Downloading is in progress......." + "</b>" + "</font>" + "</html>"; 
lbldownload.setText(str);
timer.start();



     }
timer=新计时器(间隔,新ActionListener(){
已执行的公共无效操作(操作事件evt){
如果(j==100){
//下载完成后会听到一声蜂鸣声
getDefaultToolkit().beep();
timer.stop();
jButton3.setEnabled(true);
pb.设定值(0);
字符串str=“”+”+“+”下载已完成。“+”+“+”+”;
lbldownload.setText(str);
}
j=j+1;
pb.设定值(j);
}
});
jButton3.setEnabled(false);
j=0;
字符串str=“”+”+“+”+”正在下载……“+”+”+”;
lbldownload.setText(str);
timer.start();
}

如果您知道输入数据的长度,请记录您已加载的数据量,并使用这两个数字之间的比率来获得百分比。定期向UI报告,以便它可以更新进度条

如果不知道预期大小,则无法估计百分比。

不要阻止EDT(事件调度线程)-发生这种情况时,GUI将“冻结”。而是为长时间运行的任务实施
SwingWorker
。有关更多详细信息,请参阅

另请参见,因为这是监视下载