//当前进度
static int currentProgress=0;
public static void main(String[] args) {
JFrame jf=new JFrame();
jf.setSize(500,100);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
//创建进度条
final JProgressBar progressBar=new JProgressBar();
//添加监听
progressBar.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("value change==="+progressBar.getValue()+"=="+progressBar.getPercentComplete());
}
});
progressBar.setPreferredSize(new Dimension(400,40));
//设置不确定模式[利用与不确定时间]
progressBar.setIndeterminate(true);
//设置进度条方向
// progressBar.setOrientation(SwingConstants.VERTICAL); //数字方向
//设置最大最小
progressBar.setMinimum(0);
progressBar.setMaximum(100);
//绘制百分百刻度
progressBar.setStringPainted(true);
panel.add(progressBar);
jf.setContentPane(panel);
jf.setVisible(true);
//多线程 每个0.5秒改变及进度
new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentProgress+=2;
if (currentProgress>100){
currentProgress=0;
}
//改变精度
progressBar.setValue(currentProgress);
}
}).start();
}
Swing测试进度条
最新推荐文章于 2021-12-03 17:03:11 发布