2015-08-08 233 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();