import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
private JButton button = null;
private JButton OKButton = null;
private JProgressBar progressBar = null;
JPanel panel = null;
private int value = 0;
public Test(){
Container contentPane = getContentPane();
progressBar = new JProgressBar();
button = new JButton("升级");
OKButton = new JButton("完成");
panel = new JPanel();
panel.add(button);
panel.add(OKButton);
JPanel panel_progress = new JPanel();
panel_progress.add(progressBar);
progressBar.setString("升级中……");
progressBar.setForeground(Color.RED);
progressBar.setValue(0);
progressBar.setStringPainted(true); //显示提示信息
progressBar.setPreferredSize(new Dimension(300,20));
// progressBar.setBorder(BorderFactory.createEtchedBorder());
//设置按钮的大小
button.setPreferredSize(new Dimension(60,30));
OKButton.setPreferredSize(new Dimension(60,30));
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
new ProgressBarThread().start();
}
});
OKButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
OKButton.setEnabled(false);
setSize(500, 500);
setLayout(new BorderLayout());
contentPane.add(panel,BorderLayout.NORTH);
contentPane.add(panel_progress,BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
class ProgressBarThread extends Thread{
public void run() {
while(value<=100){
value += 10;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.setValue(value);
if(value == 100){
button.setEnabled(false);
OKButton.setEnabled(true);
}
}
}
}
public static void main(String[] args) {
new Test();
}
}
有什么不足的地方欢迎大家指导……