更新 JProgressBar

发布于 2024-09-08 07:19:21 字数 657 浏览 13 评论 0原文

我无法更新我的进度条...这是我的代码

Thread t=new Thread(new Runnable(){
        public void run(){
            int i=1;
            jProgBar.setMinimum(0);
            jProgBar.setMaximum(100);
            try {
                while(i<=100 || true){
                    jProgBar.setValue(i);
                    i++;
                    Thread.sleep(50);
                }
            }
            catch (InterruptedException ex){
                jProgBar.setValue(jProgBar.getMaximum());
            }
        }
    });
    t.start();

    .... Something code that correctly works

    t.interrupt();

进度条状态仅在线程结束时更新。 有人可以帮助我吗?

I can't update my progressbar... this is my code

Thread t=new Thread(new Runnable(){
        public void run(){
            int i=1;
            jProgBar.setMinimum(0);
            jProgBar.setMaximum(100);
            try {
                while(i<=100 || true){
                    jProgBar.setValue(i);
                    i++;
                    Thread.sleep(50);
                }
            }
            catch (InterruptedException ex){
                jProgBar.setValue(jProgBar.getMaximum());
            }
        }
    });
    t.start();

    .... Something code that correctly works

    t.interrupt();

The progress bar state is updated only at the end of thread.
Can someone help me??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

乙白 2024-09-15 07:19:22

在睡眠之前,添加对 SwingUtilties.invokeLater() 的调用,该调用会生成一个线程以在 EDT 中的进度条上触发 firePropertyChange。

Before the sleep, add a call to SwingUtilties.invokeLater() that spawns a thread to fire a firePropertyChange on the progressbar in the EDT.

哭了丶谁疼 2024-09-15 07:19:22

直接使用模型而不是 JProgressBar:

DefaultBoundedRangeModel model = new DefaultBoundedRangeModel();
JProgressBar bar = new JProgressBar(model);

// Somewhere else, perhaps in another Thread
model.setValue(i)

以下示例运行良好:

public static void main(String[] args) throws InterruptedException {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 100);
    frame.setVisible(true);
    final DefaultBoundedRangeModel model = new DefaultBoundedRangeModel();
    frame.add(new JProgressBar(model));
    Thread t = new Thread(new Runnable() {
        public void run() {
            int i = 1;
            model.setMinimum(0);
            model.setMaximum(100);
            try {
                while (i <= 100 || true) {
                    model.setValue(i);
                    i++;
                    Thread.sleep(50);
                }
            } catch (InterruptedException ex) {
                model.setValue(model.getMaximum());
            }
        }
    });
    t.start();

    Thread.sleep(2000);

    t.interrupt();
}

Use a model instead of the JProgressBar directly:

DefaultBoundedRangeModel model = new DefaultBoundedRangeModel();
JProgressBar bar = new JProgressBar(model);

// Somewhere else, perhaps in another Thread
model.setValue(i)

The following example works fine:

public static void main(String[] args) throws InterruptedException {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 100);
    frame.setVisible(true);
    final DefaultBoundedRangeModel model = new DefaultBoundedRangeModel();
    frame.add(new JProgressBar(model));
    Thread t = new Thread(new Runnable() {
        public void run() {
            int i = 1;
            model.setMinimum(0);
            model.setMaximum(100);
            try {
                while (i <= 100 || true) {
                    model.setValue(i);
                    i++;
                    Thread.sleep(50);
                }
            } catch (InterruptedException ex) {
                model.setValue(model.getMaximum());
            }
        }
    });
    t.start();

    Thread.sleep(2000);

    t.interrupt();
}
烟─花易冷 2024-09-15 07:19:22

针对您的情况的最佳建议是使用 SwingWorker。查看 API http://java.sun .com/javase/6/docs/api/javax/swing/SwingWorker.html

重写流程方法以更新进度条的值(然后它将在 EDT 上正确完成)

更多信息可以在 http://java.sun.com/products/jfc/tsc/articles /threads/threads2.html

The best advice for your situation is to use SwingWorker. Check out the API at http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html

Override process method to update value of progress bar ( then it will be done correctly on EDT)

More info can be obtained at http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

旧城空念 2024-09-15 07:19:22

根据 eugener 的说法,SwingWorker 绝对是您想要在此处使用的工具,或者任何时候生成长时间运行的任务时可能会在完成之前锁定 GUI 的工具。 Sun^H^H^HOracle 提供了有关在 SwingWorker 中使用进度条的完整教程:

http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html

Per eugener, SwingWorker is definitely what you want to be using here, or any time a long-running task is spawned that could otherwise lock up your GUI prior to completion. A full tutorial on using progress bars with SwingWorker is available from Sun^H^H^HOracle here:

http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html

伴梦长久 2024-09-15 07:19:22

谢谢大家。
我是这样解决的

try{
       jProgBar.setIndeterminate(true);
       jProgBar.setStringPainted(true);
       jProgBar.setBorderPainted(true);
       new Thread(new Runnable() {
           public void run() {
               ...
               // here is code that i've to wait
               // after this i stop my jProgressBar
               ...
               jProgBar.setStringPainted(false);
               jProgBar.setBorderPainted(true);
               jProgBar.setIndeterminate(false);
       }
       }).start();
   }
   catch(IllegalStateException ex){
       //some code
   }

Thanks All.
I solved in this way

try{
       jProgBar.setIndeterminate(true);
       jProgBar.setStringPainted(true);
       jProgBar.setBorderPainted(true);
       new Thread(new Runnable() {
           public void run() {
               ...
               // here is code that i've to wait
               // after this i stop my jProgressBar
               ...
               jProgBar.setStringPainted(false);
               jProgBar.setBorderPainted(true);
               jProgBar.setIndeterminate(false);
       }
       }).start();
   }
   catch(IllegalStateException ex){
       //some code
   }
病女 2024-09-15 07:19:22

将此代码片段放在

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        jProgBar.repaint();
    }
}

“i++”和“Thread.sleep()”之间应该可以完成这项工作。要使其编译,请将 jProgBar 标记为“最终”。

Putting this snippet

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        jProgBar.repaint();
    }
}

Between 'i++' and 'Thread.sleep()' should do the job. To get it compiling, mark jProgBar as 'final'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文