-1

我在 java 中创建了一个 ProgressBar。我可以使用 pb.setValue(i); 设置值 我是一个 int 我无法设置 'i' 的值来增加进度条的值,以便在 10 秒内进度条将在 10 秒内达到 1% 到 100%。我怎样才能做到这一点 ?任何帮助,将不胜感激

4

3 回答 3

1

您可以使用计时器

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TimerTest {
public static void main(String[] args) {

    final JFrame frame = new JFrame();
    final JProgressBar ps = new JProgressBar();
    final Timer timer = new Timer(100, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int value = ps.getValue() + 1;
            ps.setValue(value);
        }

    });

    ps.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            if (ps.getValue() == 100) {
                timer.stop();
                frame.dispose();
            }
        }
    });
    frame.setSize(600, 400);
    frame.add(ps);
    frame.pack();
    frame.setVisible(true);
    timer.start();
}

}

于 2013-03-06T20:43:28.563 回答
0

查看 java 标准库中有特定时间的函数,例如此代码片段。

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

long duration = endTime - startTime;

您设置结束和开始时间,使其产生持续时间或 10 或 1 个部分,具体取决于您实现代码的方式。

于 2013-03-06T20:32:34.513 回答
0

您有任意数量的选择,这里有两个。

您可以建立一个每秒计时的计时器,直到计时 10 次,或者您可以计算经过的时间量并根据目标时间经过的时间生成进度值,这将产生更平滑的进度。 .

public class ProgressOverTime {

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

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

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                frame.add(new TestPane(), gbc);
                frame.add(new TimeGapPane(), gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class TestPane extends JPanel {

        private JProgressBar pb;
        private int progress;
        private int target = 10;

        public TestPane() {
            setLayout(new GridBagLayout());
            pb = new JProgressBar();
            pb.setMaximum(10);
            add(pb);

            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    progress++;
                    if (progress > target) {
                        ((Timer)e.getSource()).stop();
                    }
                    pb.setValue(progress);
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

    }

    public class TimeGapPane extends JPanel {

        private JProgressBar pb;
        private long startTime = -1;
        private int target = 10000;

        public TimeGapPane() {
            setLayout(new GridBagLayout());
            pb = new JProgressBar();
            pb.setMaximum(100);
            add(pb);

            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (startTime < 0) {
                        startTime = System.currentTimeMillis();
                    }
                    long now = System.currentTimeMillis();
                    long ticks = now - startTime;

                    float progress = (float)ticks / (float)target;
                    if (progress >= 1f) {
                        ((Timer)e.getSource()).stop();
                        progress = 1f;
                    }
                    pb.setValue(Math.round(100f * progress));
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

    }

}
于 2013-03-06T20:40:36.847 回答