In this example, we will use JProgressBar class to create a simple progress bar.
Trong ví dụ này, chúng ta sẽ sử dụng JProgressBar lớp để tạo ra một thanh tiến trình đơn giản.
JProgressDemo.java
/**
* @(#)JProgressDemo.java
*
* JProgressDemo application
*
* @author BUI NGOC SON
* @version 1.00 2014/5/1
*/
import java.awt.*;
import javax.swing.*;
public class JProgressDemo {
public static void main(String[] args) {
final int MAX = 100;
final JFrame frame = new JFrame("JProgress Demo");
// creates progress bar
final JProgressBar pb = new JProgressBar();
pb.setMinimum(0);
pb.setMaximum(MAX);
pb.setStringPainted(true);
// add progress bar
frame.setLayout(new FlowLayout());
frame.getContentPane().add(pb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
// update progressbar
for (int i = 0; i <= MAX; i++) {
final int currentValue = i;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pb.setValue(currentValue);
}
});
java.lang.Thread.sleep(100);
} catch (InterruptedException e) {
JOptionPane.showMessageDialog(frame, e.getMessage());
}
}
}
}
* @(#)JProgressDemo.java
*
* JProgressDemo application
*
* @author BUI NGOC SON
* @version 1.00 2014/5/1
*/
import java.awt.*;
import javax.swing.*;
public class JProgressDemo {
public static void main(String[] args) {
final int MAX = 100;
final JFrame frame = new JFrame("JProgress Demo");
// creates progress bar
final JProgressBar pb = new JProgressBar();
pb.setMinimum(0);
pb.setMaximum(MAX);
pb.setStringPainted(true);
// add progress bar
frame.setLayout(new FlowLayout());
frame.getContentPane().add(pb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
// update progressbar
for (int i = 0; i <= MAX; i++) {
final int currentValue = i;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pb.setValue(currentValue);
}
});
java.lang.Thread.sleep(100);
} catch (InterruptedException e) {
JOptionPane.showMessageDialog(frame, e.getMessage());
}
}
}
}
Kết quả - Result
No comments:
Post a Comment