我正在浏览这个帖子
How to make Timer countdown along with progress bar?
我想将此添加到我的代码中,这样我就可以获得一个jProgressBar和一个Button,(最好使用netbeans)
因此,当我按下按钮时,它会稳定地从0到100运行,我真的试图自己动手并且非常生气,任何帮助都会很好。
答案 0 :(得分:6)
利用@ Andrew的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();
}
});
}
}