简体   繁体   English

JProgressBar更新值的行为(线程)

[英]JProgressBar updating values behavior (Threaded)

I cannot seem to understand why when working with certain values the JProgressBar will sort of freeze and stop making calculations. 我似乎无法理解为什么在使用某些值时JProgressBar会冻结并停止进行计算。 For example the code bellow will only show zero all of the time; 例如,下面的代码始终只显示零;

     ...
       int value = (100/maxGenerations)*i;       //maxGenerations = 2500
       final int barValue = value;
       SwingUtilities.invokeLater(new Runnable() {
          public void run() {
          progressBar.setValue(barValue);
        }
     });

however if with the same implementation i do the following it works (of course not accurately as is just garbage calculations): 但是,如果使用相同的实现,我可以执行以下操作(当然,不准确的只是垃圾计算):

     ...
       int value = (i/100);         //where 'i' increments until = 2500
       final int barValue = value;
       SwingUtilities.invokeLater(new Runnable() {
          public void run() {
          progressBar.setValue(barValue);
        }
     });

any ideas why? 有什么想法吗?

If you know what the range of progress is (presumably 0 - 2500) you can tell the JProgressBar via progressBar.setMaximum(2500) . 如果知道进度的范围是多少(大概是0-2500),则可以通过progressBar.setMaximum(2500)告诉JProgressBar。 This way you don't have need a barValue variable, just let the progress bar know your real progress. 这样,您就不需要barValue变量,只需让进度条知道您的实际进度即可。

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

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