2013-10-05 50 views
1

我在jframe中製作遊戲,我不知道如何使用applet和製作內部類,所以我一直使用jframe風格。 這是我的問題。我如何在jframe中的組件之間添加延遲。如何在JFrame組件之間添加延遲

我在框架的中心添加了一個「loading.gif」。和一個jbutton「繼續」在框架的南部,但我想添加延遲3秒,然後「繼續按鈕」出現,並且「loading.gif」消失。 (如在遊戲的加載部分)。

,這是我的代碼...

//the p2 components 
      ImageIcon loading = new ImageIcon("C:\\java pics\\loading.gif"); 
      JLabel startLoading = new JLabel(loading); 
      JButton continue1 = new JButton("Continue"); 
      p2.add(startLoading,borderlayout.center); 
      p2.add(continue1,borderlayout.south); 

//將JButton繼續事件

start.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      // TODO Auto-generated method stub 
      f.add(p2); 
      f.remove(p1); 
      f.setVisible(true); 
      f.revalidate(); 
      f.repaint(); 

      Timer t = new Timer(); 
      //delay for 3 seconds 
      try { 
      Timer.delay(3000); 
      } catch (InterruptedException ee) { 
      return; 
      } 
      t.start();  
     } 
    }); 

我會明白任何幫助。

回答

1

but i want to add delay for 3 seconds before the "continue button" appears and the "loading.gif" disappear.

以下是你需要做的:
1.只顯示在JLABEL的GIF,當你開始遊戲。
2.產卵TimerTask只會執行一次顯示您的JFRAME後3秒。
3.定時任務中,調用上JFRAMEinvalidate()然後取出JLABEL,叫invalidate()然後添加JBUTTON

BOOM!你很高興與一個驚人的加載屏幕。
這裏是教程:http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

+2

不要使用TimerTask。 Timer是Swing使用的正確類。 – camickr

+0

你能告訴我通過編輯// jbutton continue事件代碼來完成TimerTask或Timer的正確方法嗎?或者給我一個例子。 –

+0

@ Java-Newbie將你鏈接到教程:) –