ProgressBar不会在Java中更改其值 [英] ProgressBar doesn't change its value in Java

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

问题描述

我有奇怪的问题。我设置了一个JProgressBar:

 私有JProgressBar progressBar; 

public void foo()
{
...
progressBar = new JProgressBar(0,100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
...
contentPane.add(progressBar);
...
}

但只有在我设置setValue函数时它才会改变它在代码中的某些地方,而不是在任何地方:

  public void foo2()
{
progressBar。的setValue(100); // working
if(...)
{
System.out.println(这些指令正在执行); //工作
progressBar.setValue(0); //不工作
}
}

那么,我做错了什么?为什么第二条指令不起作用?

解决方案

进度条的值确实已更新。但它还不仅仅是在屏幕上。通常,我们在循环中使用进度条。但是,当您处于循环中时,您可能通过单击按钮来强调它,它不会被绘制。为什么?因为您通过单击按钮来调用它。当您单击按钮时,您为该按钮创建的所有代码都由 AWTEventThread 执行。这是跟踪所有Swing组件的相同线程,并检查它们是否必须重新绘制。这是让你的JFrame活跃起来的线程。当你悬停一个按钮并且颜色稍微改变时,它由 AWTEventThread 完成。



所以,当你正在循环中工作,AWTEventThread无法再更新屏幕。



这意味着有两种解决方案:


  1. (推荐)您应该创建一个执行循环的单独线程。这意味着AWTEventThread可以在必要时更新屏幕(当你调用 bar.setValue(...);

      public void yourButtonClickMethod()
    {
    Runnable runner = new Runnable()
    {
    public void run(){
    //这里带有循环的原始代码。
    }
    };
    线程t =新线程(runner,Code Executer);
    t.start();
    }


  2. 手动重绘进度条。我总是用 bar.repaint(); 来做,但我想知道它是否会起作用。我虽然是那种方法。如果这不起作用,请尝试: bar.update(bar.getGraphics());



I have strange problem. I set a JProgressBar:

private JProgressBar progressBar;

public void foo()
{
    ...
    progressBar = new JProgressBar(0, 100);
    progressBar.setValue(0);
    progressBar.setStringPainted(true);
    ...
    contentPane.add(progressBar);
    ...
}

But it changes only when I put setValue function it in some places in code, not everywhere:

public void foo2()
{
    progressBar.setValue(100); //working
    if(...)
    {
        System.out.println("These instructions are executing"); //working
        progressBar.setValue(0);                                //not working
    }                             
}

So, what am I doing wrong? Why the second instruction doesn't work?

解决方案

The value of the progress bar is really updated. But it isn't simply on the screen yet. Often, we use progress bars in loops. But, while you are in the loop, which you probably invoked by clicking a button it isn't painted. Why? Because you invoked it by clicking a button. When you click a button, all the code you've made for that button is being executed by the AWTEventThread. This is the same thread that keep track of all the Swing components, and checks wether they have to be repainted. That is the thread that makes your JFrame come alive. When you hover a button and the color changes a bit, it's done by the AWTEventThread.

So, while you are working in the loop, the AWTEventThread can't update the screen anymore.

This means there are two solutions:

  1. (Recommend) You should create a separate thread which executes the loop. This means the AWTEventThread can update the screen if necessary (when you call bar.setValue(...);)

    public void yourButtonClickMethod()
    {
        Runnable runner = new Runnable()
        {
            public void run() {
            //Your original code with the loop here.
            }
        };
        Thread t = new Thread(runner, "Code Executer");
        t.start();
    }
    

  2. Manually repaint the progress bar. I did it always with bar.repaint(); but I'm wondering if it will work. I though it was that method. If that doesn't work, try: bar.update(bar.getGraphics());.

这篇关于ProgressBar不会在Java中更改其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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