仅在屏幕上显示JFrame时执行代码执行较早

问题描述:

我有一个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

可以在一个窗口监听器添加到框架和窗口打开时更新进度。下面是一个例子:

splash_window.addWindowListener(new WindowListener() { 

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

     ... 

    });