我正在浏览这个线程
如何让定时器倒计时并带有进度条? https://stackoverflow.com/questions/5931933/how-to-make-timer-countdown-along-with-progress-bar
我想将其添加到我的代码中,这样我就可以获得一个 jProgressBar 和一个按钮,(最好使用 netbeans)
因此,当我按下按钮时,它会从 0 稳定地运行到 100,我真的尝试过自己做这件事,但我真的很生气,任何帮助都会很好。
利用@Andrew的example https://stackoverflow.com/a/5932222/230513,
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();
}
});
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)