2012-05-10 124 views
2
private void OptionsActionPerformed(java.awt.event.ActionEvent evt) 
{ 
// After clicking on button X, I want 4 other buttons to show up 
// in a sequential order 

ButtonTrue(); 
} 


public void ButtonTrue() 
{ 
    Audio_Options.setVisible(true); 
    letsSleep(); 
    Control_Options.setVisible(true); 
    letsSleep(); 
    Display_Options.setVisible(true); 
    letsSleep(); 
    Network_Options.setVisible(true); 
} 

public void letsSleep() 
{ 
    try { 
     Thread.sleep(10000); 
    } catch (InterruptedException ex) { 
     Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

我有4個按鈕。我希望它們按順序排列,如: Button1 - 10秒 - Button2 - 10秒 - Button3 - 10seconds - Button 4Swing:啓用延遲按鈕

問題:每當我調用函數「ButtonTrue()」時,它們都會一起出現等待30秒。什麼會導致這個問題發生?

+0

你從哪裏調用ButtonTrue? –

+0

private void OptionsActionPerformed(java.awt.event.ActionEvent evt)// TODO在這裏添加處理代碼: ButtonTrue(); } 當單擊另一個按鈕時,將調用ButtonTrue()以啓用這4個按鈕。 – Encinaar

回答

5
+0

我會讓它更通用,永遠不要在EDT上使用'Thread.sleep'。也許一個額外的鏈接到[Swing併發教程](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – Robin

0

你應該使用不同的線程這樣的:

javax.swing.Timer timer = new Timer(10000, new ActionListener() { 
    public void actionPerformed(ActionEvent evt) { 
    //...Update the progress bar... 
     Control_Options.setVisible(true); 

     timer.stop(); 

    }  
}); 
timer.start(); 

你的按鈕必須最後是在匿名ActionListener的範圍內。

0

我認爲問題是所有的setVisble調用都在一個線程內,而不是EventDispatchThread。你可以嘗試撥打電話:

if(EventQueue.isDispatchThread()) { 
    ButtonTrue(); 
} else { 
    EventQueue.invokeAndWait(new Runnable() { 
     ButtonTrue(); 
    }); 
} 
+0

我試過你的方法,但仍然沒有改變。 – Encinaar

+0

@ user1183220是的,我猜這是不對的。我編輯了我的帖子。也許它現在工作 – sebastian