当前位置: 代码迷 >> J2SE >> 测试JProgressBar的有关问题,为什么用一个JButton来控制就会出错
  详细解决方案

测试JProgressBar的有关问题,为什么用一个JButton来控制就会出错

热度:64   发布时间:2016-04-24 14:59:31.0
测试JProgressBar的问题,为什么用一个JButton来控制就会出错?
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();
}
});