Java Swing 之Timer配合JProgressBar的使用
阿新 • • 發佈:2019-01-27
views star borde ane start str led 零基礎 blog
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
Java Swing 之Timer配合JProgressBar的使用