2017-04-05 33 views
0

我有一個JFrame調用splash_window,我用它作爲啓動屏幕爲我的應用程序,它首先顯示。它有一個progress bar,它最初在0%,我希望它更改爲5%,並在用戶可見時立即執行一些其他命令。
當我運行的應用程序應用程序,the progress bar的進步是已經5%splash screen可見之前,我可以在正在執行我的其他命令控制檯中看到。
我的問題是:我怎樣才能讓這個命令一旦splash screen對屏幕上的用戶可見就立即執行?僅在屏幕上顯示JFrame時執行代碼執行較早

我的代碼:

//making the splash_window visible after adding all components to it 
splash_window.setVisible(true); 

//Used to detect when the splash_window is visible to the user 
if (splash_window.isShowing()){ 

    //Used to know if the splash_window is visible 
    System.out.print("Splash screen is complete and now visible." + System.lineSeparator()); 

    //Method used to increase my progress by 5% 
    initProgress.setValue(getProgress(5)); 

    //Other commands... 
} 
+0

1)對於s閃屏,看['SplashScreen'](http://docs.oracle.com/javase/8/docs/api/java/awt/SplashScreen。 html)2)爲了更好地幫助您,請發佈[MCVE]或[Short,Self Contained,Correct Example](http://www.sscce.org/)。 –

+1

使用WindowListener和Timer的組合,因爲WindowListener可能會在由於OS開銷而在屏幕上實際實現窗口之前發出通知 – MadProgrammer

回答

2

可以在一個窗口監聽器添加到框架和窗口打開時更新進度。下面是一個例子:

splash_window.addWindowListener(new WindowListener() { 

     @Override 
     public void windowOpened(WindowEvent e) { 
      initProgress.setValue(getProgress(5)); 
      initProgress.revalidate(); 
     } 

     ... 

    });