-1

JavaでProgressBarを作成しました。pb.setValue(i); を使用して値を設定できます。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

選択肢はいくつもありますが、ここでは 2 つです。

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 に答える