JProgressBar太快了 [英] JProgressBar too fast

查看:156
本文介绍了JProgressBar太快了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加进度条。一切正常,我没有任何错误。但进度条从0%变为100%,甚至没有通过它之间的值(我的意思是它太快了,用户无法看到进度条块填写)

I am trying to add a progress bar. everything works and i don't get any error. But the progress bar goes from 0% to 100% without even going through the values between it (I mean it's too fast, and the users are unable to see the progress bar blocks filling in)

pr = new JProgressBar();

            pr(0);
            pr(true);
..

public void iterate(){

        while (i<=20000){
            pr.setValue(i);
            i=i+1000;
            try{
                Thread.sleep(150);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

点击i按钮时调用 iterate()方法,我希望它能逐步更新进度条。相反,它会暂停一段时间,然后显示一个完整的进度条。

When i button is clicked i call the iterate() method, and i expect it to update the progress bar progressively. instead it pauses for a while and then displays a full progress bar.

我该如何解决这个问题?

How can i solve this ?

2.)我不喜欢进度条选项卡的默认蓝色。我需要改变颜色。我试过 pr.setForeground(Color.GRAY);
pr.setBackground(Color.RED);
但它没有用。

2.) I don't like the default blue color of the progress bar tabs. I need to change the color. I tried pr.setForeground(Color.GRAY); pr.setBackground(Color.RED); But it didn't work.

推荐答案

<问题是,你正试图在事件调度线程的上下文中更新进度。

The problem is, you're trying to update the progress within the context of the Event Dispatching Thread.

这基本上意味着当你在循环中时, EDT无法处理您正在进行的任何绘制请求。

This basically means that while you are in you loop, the EDT is unable to process any paint request you are making.

您需要做的是如何将工作卸载到单独的线程并根据需要更新进度条。这个问题,你永远不应该从EDT之外的任何线程更新UI。

What you need to do is some how offload the work to a separate thread and update the progress bar as needed. The problem with this, is you should never update the UI from any thread other then the EDT.

但是不要绝望,你有很多选择,最好的正在使用 Swing Worker

But don't despair, you have a number of options, the best is using a Swing Worker

更新了示例

public class SwingWorkerProgress {

    public static void main(String[] args) {
        new SwingWorkerProgress();
    }

    public SwingWorkerProgress() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JProgressBar pbProgress;
        private JButton start;

        public TestPane() {

            setBorder(new EmptyBorder(10, 10, 10, 10));
            pbProgress = new JProgressBar();
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(pbProgress, gbc);

            start = new JButton("Start");
            gbc.gridy++;
            add(start, gbc);

            start.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    start.setEnabled(false);
                    ProgressWorker pw = new ProgressWorker();
                    pw.addPropertyChangeListener(new PropertyChangeListener() {

                        @Override
                        public void propertyChange(PropertyChangeEvent evt) {
                            String name = evt.getPropertyName();
                            if (name.equals("progress")) {
                                int progress = (int) evt.getNewValue();
                                pbProgress.setValue(progress);
                                repaint();
                            } else if (name.equals("state")) {
                                SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
                                switch (state) {
                                    case DONE:
                                        start.setEnabled(true);
                                        break;
                                }
                            }
                        }

                    });
                    pw.execute();
                }
            });

        }
    }

    public class ProgressWorker extends SwingWorker<Object, Object> {

        @Override
        protected Object doInBackground() throws Exception {
            int i = 0;
            int max = 2000;

            while (i < max) {
                i += 10;
                int progress = Math.round(((float)i / (float)max) * 100f);
                setProgress(progress);
                try {
                    Thread.sleep(25);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            return null;
        }
    }
}

这篇关于JProgressBar太快了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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