Thread.sleep(1000)无法在Swing中工作

Pra*_*ara 1 java animation swing multithreading

我在java swing中有一个简单的动画程序.但它没有用.

    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){}
Run Code Online (Sandbox Code Playgroud)

我在netbeans 7.1工作.

Ext*_*ers 5

从您的代码中我了解到您正试图通过增加(升级)其大小来设置图标动画.但是,由于睡眠任务是在事件调度线程(EDT)上完成的,因此会导致GUI冻结.因此,不应该在Event Dispatch Thread上运行Thread.sleep()等任务.

考虑使用SwingUtilities计时器