jprogressbar is non tegrant
因此,我正在尝试制作一个下载器,该下载器显示了使用进度栏的下载进度。 但是我有问题,因为它实际上并没有更新进度栏。基本上,当它是蓝色的时候,它会保持白色。如果有人可以提供帮助,则代码在下面。
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){}
}
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){}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如@happyburnout所指出的那样,您会更好地在单独的线程中使用 swingworker 可能是您正在做的事情的最佳解决方案。
主要原因是您要阻止事件派遣线程(( aka edt)运行,防止处理任何重新粉刷请求(和其他UI重要的事情)。
您应该通过
现在,这几乎直接从API文档中获取,但是用
swingwoker
给出一个jprogressbar
“ worker” ...
“ progress pane”
的黄金规则
thread
其他的组件,那么EDTthread
上执行耗时的任务您将在摇摆方面度过快乐,轻松的时光:D
As @happyburnout has pointed out, you'd be better of processing you download in a separate thread, using a SwingWorker is probably the best solution for what you are doing.
The main reason is you're blocking the Event Dispatching Thread (AKA EDT) from running, preventing any repaint requests (and other UI important things) from been processed.
You should have a read through
Now this is taken almost directly from the API docs, but gives a basic idea of a
SwingWoker
with aJProgressBar
The "Worker"...
The "progress pane"
Remember the golden rules of Swing
Thread
other then the EDTThread
And you will have a happy and easy time with Swing :D
与Swingworker结合使用。
在此处查看一个示例:
“ nofollow”> swingworker and Progress bar
@hovercraft @hhovercraft:@hhovercraft:对您。请允许我参考相应的,我认为这最好解释情况。
Use a combination with SwingWorker.
See an example here:
SwingWorker and Progress Bar
@Hovercraft: You're right. Allow me to refer to the corresponding SwingWorker page of JavaDoc, in my opinion this explains the situation best.