简体   繁体   English

如何在Java中更新Swing JProgressBar

[英]How to Update Swing JProgressBar in Java

I'm really new to Java, especially working with GUI in Java. 我对Java真的很陌生,尤其是在Java中使用GUI。 I want to put a progress bar in my program, where it updates its value each time after certain amount of work has been done. 我想在程序中放置一个进度条,每次完成一定数量的工作后,每次更新它的值。 Right now I have a button that executes a method when pressed, and I put setValue() function (with the value that I want) each time after certain work has been done in that method. 现在,我有一个按钮,当按下该按钮时将执行一个方法,并且每次在该方法完成某些工作后,我都会将setValue()函数(具有所需的值)放入该方法中。 However, when I press the button, the button seems to be stuck pressed while the method continues (the method takes a while to finish executing), and the progress bar doesn't get updated until all the other things has been done in the ButtonListener, so it goes directly to 100% after it's done). 但是,当我按下按钮时,方法继续执行时该按钮似乎一直处于按下状态(该方法需要一段时间才能完成执行),并且直到在ButtonListener中完成所有其他操作后进度条才会更新,因此完成后,它会直接达到100%)。

What am I doing wrong? 我究竟做错了什么?

Thanks in advance. 提前致谢。

That is, most probably, because you are doing all work in main thread so it hangs your UI until all work is done. 那是最有可能的原因,因为您正在主线程中完成所有工作,因此它会挂起您的UI,直到完成所有工作为止。 You should not do this. 您不应该这样做。 You should use separate EDT for such operations. 您应该对此类操作使用单独的EDT What you should try is do your update progressBar job in separate EDT. 您应该尝试在单独的EDT中执行更新progressBar作业。 Use SwingUtilities.invokeLater(Runnable r) for this. 为此使用SwingUtilities.invokeLater(Runnable r)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM