2016-09-02 28 views
0

嗨,大家好,我在動畫之間添加了一個定時器,問題與我的代碼是最後一個動畫必須等待足夠的時間,以便讓以前的動畫結束,代碼在Java動畫中添加一個定時器

if (array1[0]>array1[1]) 
    { 
    AnimationClass a = new AnimationClass(); 
    a.jButtonYUp(110, 30, 100,1, jButton6); 
    a.jButtonXLeft(250, 50, 100, 1, jButton6); 
    a.jButtonXRight(50, 250, 100, 1, jButton5); 

    // I need the timer here about 2 seconds 

    a.jButtonYDown(30, 110, 100, 1, jButton2); 

    }  

所以我將不勝感激,如果如果它已經在那裏有人能指點我的問題或回答這個問題對我來說

BTW這AnimationClass是由你塊莖開發的庫和我目前正在關注他的教程

回答

0
if (array1[0]>array1[1]) 
    { 
    AnimationClass a = new AnimationClass(); 
    a.jButtonYUp(110, 30, 100,1, jButton6); 
    a.jButtonXLeft(250, 50, 100, 1, jButton6); 
    a.jButtonXRight(50, 250, 100, 1, jButton5); 

    Timer timer1 = new Timer(); 
    MyTimerTask timer1_task = new MyTimerTask(); 
    timer1.schedule (timer1_task, 2000, 0); 

    } 



class MyTimerTask extends TimerTask 
{ 
    public void run() 
    { 
    a.jButtonYDown(30, 110, 100, 1, jButton2); 
    } 
} 
+0

我認爲這需要一點點的修訂自'了'的作用範圍是'if'。 – ChiefTwoPencils

+0

正是我正在尋找 –

-1

您可以使用Thread.sleep()來延遲執行。

if (array1[0]>array1[1]) 
    { 
    AnimationClass a = new AnimationClass(); 
    a.jButtonYUp(110, 30, 100,1, jButton6); 
    a.jButtonXLeft(250, 50, 100, 1, jButton6); 
    a.jButtonXRight(50, 250, 100, 1, jButton5); 

    try 
    { 
     Thread.sleep(2 * 1000); // 2 seconds 
    } catch (InterruptedException e) 
    { 
     e.printStackTrace(); 
    } 

    a.jButtonYDown(30, 110, 100, 1, jButton2); 

    }