import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Progress extends JFrame {
JProgressBar current;
JButton find = new JButton( "开始 ");
int num = 0;
public Progress(){
super( "progress ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
current = new JProgressBar(0,100);
current.setValue(0);
current.setStringPainted(true);
panel.add(current);
panel.add(find);
getContentPane().add(panel);
find.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
interate();
}
});
}
public void interate(){
while(num <=100){
current.setValue(num);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
num++;
}
}
public static void main(String[] args) {
Progress p = new Progress();
p.pack();
p.setVisible(true);
//p.interate();
}
}
------解决方案--------------------
/********************第21行起换成我这样就行了
find.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
new Thread(new Runnable(){
public void run(){
interate();
}
}).start();
}
});