JProgressBar 不会在循环内更新 [英] JProgressBar does not update within the loop

查看:55
本文介绍了JProgressBar 不会在循环内更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在自学 Java.我一直在尝试不同的东西,比如 JRadioButtons、JcomboBoxes 等.现在,我正在尝试使用 JProgressBars 但它没有似乎工作正常.

I am currently self-studying Java. I've been trying different things like JRadioButtons, JcomboBoxes etc. Now, I'm trying to use JProgressBars but it doesn't seem to work properly.

相关代码:

JProgressBar progress;
JButton button;     //Fields of the class

progress=new JProgressBar(JProgressBar.HORIZONTAL,0,100);
button=new JButton("Done");    //Done from methods

progress.setValue(0);
progress.setStringPainted(true);
progress.setBorderPainted(true);  //Also done from methods

button.addActionListener(this);   //Also done from methods

button 被点击时,我想显示 JProgressBar 从 0% 到 100%.下面是 actionPerformed 方法的相关部分:

When button is clicked, I want to show the JProgressBar go from 0% to 100%. Here is the relevant portion of the actionPerformed method:

public void actionPerformed(ActionEvent e)
{

        for(int i=0;i<=progress.getMaximum();i++)
        {
            progress.setValue(i);
            /*try{
                Thread.sleep(10);
            }catch(InterruptedException ex)
            {
                System.err.println("An error occured:"+ex);
                ex.printStackTrace();
            }*/
        }


        progress.setValue(progress.getMinimum());
}

我已将 progressbutton 添加到 JPanel 中,并将面板添加到我使用 setVisible(true); 的 JFrame 中.

I've added both progress and button into a JPanel and the panel into a JFrame on which I use setVisible(true);.

这里的问题是,每当我按下 JButton button 时,JProgressBar progress 不会从 0% 变为 100%.相反,什么也没有发生.如果我取消注释 try...catch 块,然后按下 button,程序会冻结"一会儿然后继续.这一次,JProgressBar 也保持在 0%,我从未看到它移动.我还尝试了 repaint(); 就在 try...catch 块之后,但发生了同样的事情.

The problem here is, whenever I press the JButton button, the JProgressBar progress does not go from 0% to 100%. Instead, nothing happens. If I uncomment the try...catch block, and then press button, the program "freezes" for a moment and then continues. This time too, The JProgressBar stays at 0% and I never see it moving. I've also tried a repaint(); just after the try...catch block but the same thing happened.

我试过添加

System.out.println(i+"");

actionPerformed 中的 for 循环内,这会在终端中打印数字 0 到 100.所以,我确定循环运行了.

inside the for loop in actionPerformed and this printed numbers 0 to 100 in the terminal. So, I'm sure that the loop runs.

我该如何解决这个问题?

How can I solve this problem?

推荐答案

  • Swing 是单线程的,

    • Swing is single threaded,

      所有更新都必须在 EDT 上完成,

      all updates must be done on EDT,

      当前 EDT 中的所有事件都在一瞬间绘制在 GUI 中,

      all events in current EDT are painted in GUI in one moment,

      Thread.sleep 在 EDT 中锁定事件的执行,重绘可以在 for_loop 结束时绘制,在所有来自 Thread.sleep 执行,更多在 Oracle 跟踪中 - Swing 中的并发EventDispatchThread

      Thread.sleep to lock execution of event in EDT, repaint can be painted at the end of for_loop, after all lock(s) from Thread.sleep are executed, more in Oracle trail - Concurency in Swing and EventDispatchThread

      AWT (Swing) Listener 的输出 - ActionListener,如果 actionPerformed 中的所有事件都被执行了,那么应该只执行 progress.setValue(progress.getMinimum());,不管有没有Thread.sleep

      output from AWT (Swing) Listener - ActionListener, should be done if all events inside actionPerformed are executed, then there is executed only progress.setValue(progress.getMinimum());, doesn't matter if is there Thread.sleep or not

      public void actionPerformed(ActionEvent e) -> progress.setValue(progress.getMinimum());
      

      <小时>

      • 使用 SwingWorkerRunnable#Threadprogress.setValue(i); 包裹到 invokeLater>


        • use SwingWorker or Runnable#Thread with progress.setValue(i); wrapped into invokeLater

          为了更好的帮助,尽快发布 SSCCE/MCVE,简短,可运行,可编译

          for better help sooner post an SSCCE/MCVE, short, runnable, compilable

          这篇关于JProgressBar 不会在循环内更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆