我在run()中,用for循环设置进度条的值,循环结束后关闭进度条所在的frame.
但是没有加上关闭窗口的语句时,一切正常。加上关闭窗口的语句后,不正常。
是否线程有什么机制问题?我无法了。
一下源代码:
public void run(){
for (int i = 1; i < 11 ; i++){
p.prog.setValue(i*100);
try{
Thread.sleep(300);
}catch(Exception e){}
}
/*try{
Thread.sleep(20000);
}catch(Exception e){}
//关闭本框架,打开密码框架
//setVisible(false);
//dispose();*/
}
}
------解决方案--------------------
将你的内部类修改如下:
class MyListener implements Runnable,WindowListener {
public void windowOpened(WindowEvent we){
p.prog.setMinimum(0);
p.prog.setMaximum(10);
run();
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
/*设置进度条*/
public void run(){
for (int i = 1; i < 11 ; i++){
p.prog.setValue(i);
p.prog.update(p.prog.getGraphics());
System.out.println(i);
try{
Thread.sleep(300);
}catch(Exception e){}
}
System.out.println( "why ");
dispose();
/*try{
Thread.sleep(2000);
}catch(Exception e){}
//关闭本框架,打开密码框架
//setVisible(false);
*/
}
}
我试了一下,没有问题了,主要是你的进度条有问题。