Timer作为java开发中常用的一个定时工具,配合JProgressBar使用起来还真是方便,只需要调用timer.start()方法就能激活并运行,然后调用stop()方法便能停止,还可以再次通过restart()方法重新使其运行,下面就直接上代码吧:
package UsefulKnowledge;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TimerAndProgressBar implements ActionListener,ChangeListener{
JFrame f=null;
JProgressBar progressBar=null;
JLabel label;
Timer timer;
JButton b;
public TimerAndProgressBar() {
// TODO Auto-generated constructor stub
f=new JFrame("ProgressBar and Timer Test");
Container containerPane=f.getContentPane();
label=new JLabel("",JLabel.CENTER);
progressBar=new JProgressBar();
progressBar.setOrientation(JProgressBar.HORIZONTAL);
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.addChangeListener(this);
progressBar.setPreferredSize(new Dimension(200,30));
JPanel panel=new JPanel();
b=new JButton("Start");
b.addActionListener(this);
panel.add(b);
timer=new Timer(50,this);
containerPane.add(panel,BorderLayout.NORTH);
containerPane.add(progressBar,BorderLayout.CENTER);
containerPane.add(label,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
public static void main(String []args){
new TimerAndProgressBar();
}
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
int value=progressBar.getValue();
if(e.getSource()==progressBar){
label.setText("目前已完成进度:"+Integer.toString(value)+"%");
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b){
timer.start();
}
if(e.getSource()==timer){
int value=progressBar.getValue();
if(value<100){
value++;
progressBar.setValue(value);
}else{
timer.stop();
progressBar.setValue(0);
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
我们在程序中设置了stateChanged方法来侦听进度条的value的改变,从而达到对其的控制。是不是感觉很方便。
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
原文地址:https://www.cnblogs.com/djuwcnhwbx/p/10325818.html
时间: 2024-10-06 20:05:32