【问题标题】:Stop for a couple of seconds a Progress Bar停止几秒钟的进度条
【发布时间】:2018-05-13 11:12:47
【问题描述】:

我想要完成的事情:

我想制作一个带有多个进度条 (JProgressBar) 且速度不同的 JFrame

问题

当一个进度条的值为 25、50 或 75 时,应该停止当前进度条 2 秒。 但截至目前,Thread.sleep() 将停止整个应用程序。

Timer timer;

ActionListener listener;
listener = new ActionListener() {
    int counter = 0;
    int delta = 1;
    int min = 0;
    int max = 100;

    public void actionPerformed(ActionEvent ae) {
        counter += delta;
        if (jRadioButton1.isSelected()) {
            if (counter < min || counter > max) {
                delta *= -1;
            }
            counter = Math.min(max, Math.max(min, counter));
            jjProgressBar.setValue(counter);
            jjProgressBar2.setValue(counter);
            jjProgressBar3.setValue(counter);
            if (jProgressBar.getValue() == 25 || jProgressBar.getValue() == 0 || jProgressBar.getValue() == 50 || jProgressBar.getValue() == 75 || jProgressBar.getValue() == 100) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Sim1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
};
timer = new Timer(100, listener);
timer.start();

【问题讨论】:

  • 1) 使用逻辑一致的形式缩进代码行和块。缩进是为了让代码流更容易理解! 2) 为了尽快获得更好的帮助,请发布minimal reproducible exampleShort, Self Contained, Correct Example。 3) 源代码中的一个空白行是永远需要的。 { 之后或} 之前的空行通常也是多余的。
  • 无法理解您想要做什么。如我所见,您有 1 个主要进度条和 3 个辅助进度条。当主进度条的值为 0、25、50、75 或 100 时,您希望仅将此进度条停止 2 秒。对吗?

标签: java multithreading swing jprogressbar


【解决方案1】:

您始终可以multithread 您的进度条和Thread.sleep() 单独使用它们。

Thread.sleep() 在您的特定上下文中并没有真正意义,因为它只是一个控制应该是 MVC/多线程应用程序的单体应用程序。

猜你喜欢
  • 1970-01-01
  • 2012-09-15
  • 2020-01-30
  • 2021-03-12
  • 2014-05-18
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多