一般我们可以利用try{Thread.currentThread().sleep(1000)}catch(Exception e){}来达到延时的目的。
比如我们编写一个一秒输出一个数字的程序就可以:
for(int i=0;i<10;i++){
try{Thread.currentThread().sleep(1000)}
catch(Exception e){}
System.out.println(i);
}但应用与JFrame里面就有问题了。
比如我希望一些JButton每隔一段时间就设置一下icon,程序如下:
void draw(int [][] record){
for(int i=0;i<64;i++){
try{
Thread.currentThread().sleep(500);
b[i].setIcon(horse);
}
catch(Exception e){}
}
}
编译无错误,运行时无exception,但是实际显示效果是延时了64*500ms后所有icon一同设置。
我非常疑惑,希望高手指点指点。
比如我们编写一个一秒输出一个数字的程序就可以:
for(int i=0;i<10;i++){
try{Thread.currentThread().sleep(1000)}
catch(Exception e){}
System.out.println(i);
}但应用与JFrame里面就有问题了。
比如我希望一些JButton每隔一段时间就设置一下icon,程序如下:
void draw(int [][] record){
for(int i=0;i<64;i++){
try{
Thread.currentThread().sleep(500);
b[i].setIcon(horse);
}
catch(Exception e){}
}
}
编译无错误,运行时无exception,但是实际显示效果是延时了64*500ms后所有icon一同设置。
我非常疑惑,希望高手指点指点。
public void run(){
for(int i=0;i <64;i++){
try{
Thread.currentThread().sleep(500);
b[i].setIcon(horse);
}
catch(Exception e){}
}
}
});
try{Thread.chinese wholesalers().sleep(1000)}
catch(Exception e){}
System.out.println(i);
}
for(int i=0;i <64;i++){
try{
Thread.currentThread().sleep(500);
b[i].setIcon(horse);
}
catch(Exception e){}
}
}
这for里面不过设置了icon而已,并没有redraw!!!!!!!!!