让我的 jProgressBar 在 1 到 100 的计时器上运行 [英] Getting my jProgressBar to run on a timer from 1 to 100

查看:23
本文介绍了让我的 jProgressBar 在 1 到 100 的计时器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看这个帖子

如何让计时器和进度条一起倒计时?

我想将此添加到我的代码中,这样我就可以获得一个 jProgressBar 和一个按钮,(最好使用 netbeans)

I would like to add this to my code so i can just get a jProgressBar and a Button, (Using netbeans preferably)

所以当我按下按钮时,它会稳定地从 0 运行到 100,我真的尝试过自己去尝试,并且非常生气,任何帮助都会很好.

So that when I hit the button it runs from 0 to 100 steadily, I really have tried to go at this on my own and have got really mad, any help would be nice.

推荐答案

利用@Andrew 的 example,

Leveraging @Andrew's example,

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

class CountUpProgressBar extends JPanel {

    private JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
    private JLabel label = new JLabel("", JLabel.CENTER);
    private Timer timer = new Timer(100, new ActionListener() {

        private int counter = 1;

        @Override
        public void actionPerformed(ActionEvent ae) {
            label.setText(String.valueOf(counter));
            bar.setValue(++counter);
            if (counter > 100) {
                timer.stop();
            }
        }
    });

    CountUpProgressBar() {
        super.setLayout(new GridLayout(0, 1));
        bar.setValue(0);
        timer.start();
        this.add(bar);
        this.add(label);
        JOptionPane.showMessageDialog(null, this);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                CountUpProgressBar cdpb = new CountUpProgressBar();
            }
        });
    }
}

这篇关于让我的 jProgressBar 在 1 到 100 的计时器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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