import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class JProgressBarDemo extends JFrame 
{
private JProgressBar progressBar;
private JButton stopButton,startButton;
private Timer timer;
public JProgressBarDemo()
{
super("进度条");
setSize(300,200);
//获取内容面板;
Container container=getContentPane();
container.setBackground(Color.YELLOW);
container.setLayout(new FlowLayout(FlowLayout.CENTER));
//try
// {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// }catch(Exception e){}
        //设置面板的背景颜色和布局;
        progressBar=new JProgressBar();
        progressBar.setMinimum(0);
        progressBar.setMaximum(100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        progressBar.setBorderPainted(true);
        progressBar.setPreferredSize(new Dimension(250,30));
        progressBar.setForeground(Color.GREEN);
        progressBar.setBackground(Color.WHITE);
        container.add(progressBar);
        //设置按钮
        startButton=new JButton("开始");
        stopButton=new JButton("暂停");
        startButton.setBackground(Color.WHITE);
        stopButton.setBackground(Color.WHITE);
        startButton.setFont(new Font("seriy",Font.PLAIN,14));
        stopButton.setFont(new Font("seriy",Font.PLAIN,14));
        stopButton.setEnabled(false);
        TimerHandler handler=new TimerHandler();
        startButton.addActionListener(handler);
        stopButton.addActionListener(handler);
        container.add(startButton);
        container.add(stopButton);
        Timer timer=new Timer(50,handler);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) 
{
JProgressBarDemo application=new JProgressBarDemo();
    }       
    class TimerHandler implements ActionListener
    {
     private int value= 0;
     public void actionPerformed(ActionEvent event)
     {
     if(event.getSource()==timer)
     {
     value=progressBar.getValue();
     if(value < 100)
     {
     value++;
     progressBar.setValue(value);
     }
     else
     {
     timer.stop();
     startButton.setEnabled(true);
     stopButton.setEnabled(false);
     }
     }
     else if(event.getSource()==startButton)
     {
     if(progressBar.getValue()>=100)
     progressBar.setValue(0);
     timer.start();
     startButton.setEnabled(false);
     stopButton.setEnabled(true);
     }
     else if(event.getActionCommand().equals("暂停"))
     {
     timer.stop();
     stopButton.setText("重启动");    
     }
     else if(event.getActionCommand().equals("重启动"))
     {
     timer.restart();
     stopButton.setText("暂停");
     }
     }    
    }
}