视频下载(1000)在Swing不工作 [英] Thread.sleep(1000) not working in Swing
问题描述
我在Java Swing的一个简单的动画PROGRAME。但它不工作。
I have a simple animation programe in java swing. But it is not working.
try{
for(int i = 1; i<=500; i++){
ImageIcon icon = new ImageIcon("img\\COVERFront.jpg");
Image image = icon.getImage();
Image scaled = image.getScaledInstance(400, i, 0);
jLabel2.setIcon(new ImageIcon(scaled));
Thread.sleep(1000);
}
}
catch(InterruptedException ie){}
我在NetBeans 7.1的工作。
I am working in netbeans 7.1.
推荐答案
从您的code我明白,你试图通过增加(倍增),其大小以动画图标。
然而,由于睡眠的任务之一是事件调度线程上完成(EDT)它会导致GUI冻结。因此,所有的时间采取的任务,如视频下载()不应该在事件指派线程上运行。
From your code I understand that you are trying to animate a icon by increasing(upscaling) its size. However since the sleeping tasks is done on the event dispatch thread(EDT) it causes the GUI to freeze. So all time taking tasks such as Thread.sleep() should not be run on the Event Dispatch Thread.
考虑使用<一个href=\"http://stackoverflow.com/questions/6343716/why-do-i-need-swing-utilities-and-how-do-i-use-it\">SwingUtilities或<一个href=\"http://stackoverflow.com/questions/1682931/how-to-make-an-animation-with-swing?rq=1\">timer
这篇关于视频下载(1000)在Swing不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!