我想用两个button,btn1执行jprogressbar的开始,btn2暂停,代码写出来但没有执行,希望高手指教
import java.awt.BorderLayout;
public class StopThreadTest extends JFrame implements Runnable{
private JPanel contentPane;
private boolean running;
private JProgressBar progressBar;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StopThreadTest frame = new StopThreadTest();
frame.setVisible(true);
new Thread(frame).start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public StopThreadTest() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
progressBar = new JProgressBar();
progressBar.setBounds(132, 64, 146, 20);
progressBar.setStringPainted(true);
progressBar.setIndeterminate(false);
contentPane.add(progressBar);
JButton btnStart = new JButton("start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_bottun_true(e);
}
});
btnStart.setBounds(73, 139, 117, 29);
contentPane.add(btnStart);
JButton btnStop = new JButton("stop");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_false(e);
}
});
btnStop.setBounds(227, 139, 117, 29);
contentPane.add(btnStop);
}
protected void do_bottun_true(ActionEvent e){
this.running=true;
}
protected void do_button_false(ActionEvent e){
this.running=false;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(running){
for(int i=0;i<=100;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressBar.setValue(i);
}
progressBar.setString("安装完成");
}
}
}