2015-08-08 188 views
3

我正在嘗試爲一個遊戲製作閃屏,它們都是JFrames。我想讓飛濺屏幕打開3秒鐘,然後處理。遊戲主要部分的JFrame需要立即創建並顯示。我使用Thread.sleep()等待3秒,但加載頁面延遲3秒而不是遊戲。代碼如下:如何解決此代碼的閃屏?

new load(); 
try 
{ 
    Thread.sleep(3000); 
    dispose(); 
    new gameInfo(); 
} 
catch (InterruptedException ex) 
{ 
    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex); 
} 
+0

我們需要更多的上下文。 「dispose」究竟做了什麼,構造'load'和'gameInfo'對象的副作用是什麼? –

+0

爲什麼不使用Java的SplashScreen? https://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html – Alanmars

回答

0

您需要在新的線程中運行它,因爲你做了什麼,現在被凍結的主線程,它影響的是圖形用戶界面,使之凍結了。所以,你需要在後臺等待3000ms,唯一簡單的方法就是創建一個新線程。這是僞代碼

new load(); 
new Thread(){ 
    public void run(){ 
     try { 
      Thread.sleep(3000); 
      //i think you should call this 2 lines below in main thread 
      dispose(); 
      new gameInfo(); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex); 
     }  
    } 
}.start(); 

此代碼不起作用,這只是僞代碼。我需要看到整個班級能夠運行。

0
new load(); 
new Thread(){ 
    public void run(){ 
     try { 
      Thread.sleep(3000); 
      //i think you should call this 2 lines below in main thread 
      dispose(); 
      new gameInfo(); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex); 
     }  
    } 
}.start();