gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:00:51 24 4
gpt4 key购买 nike

我在我的应用程序中使用进度条,从网站下载内容并将其保存到我的数据库中。我已经放置了一个进度条来指示下载是否完成,但问题是当所有内容都保存在我的数据库中时,进度条变为 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();



}

最佳答案

不要阻塞 EDT(事件调度线程)——发生这种情况时 GUI 将“卡住”。相反,为长时间运行的任务实现一个 SwingWorker。请参阅Concurrency in Swing了解更多详情。

另请参阅ProgressMonitorInputStream ,因为这是监视下载。

关于java - 如何使用进度条从网站下载应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com