仅当 JFrame 在屏幕上可见时才执行代码
Execute code only when JFrame is visible on screen executes earlier
我有一个名为 splash_window
的 JFrame
,我将其用作我的应用程序的 启动画面,它首先显示。它有一个 progress bar
最初位于 0%
,我希望它更改为 5%
并在 用户可见后立即执行一些其他命令.
当我 运行 应用程序时,the progress bar
的进度是 已经 在 5%
之前 我的splash screen
变得可见,我可以在控制台中看到我的其他命令正在执行。
我的问题是:如何才能使命令在 splash screen
对用户 在屏幕上 ?
可见时立即执行
我的代码:
//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...
}
您可以向框架添加一个 window 侦听器,并在 window 打开时更新进度。这是一个例子:
splash_window.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
initProgress.setValue(getProgress(5));
initProgress.revalidate();
}
...
});
我有一个名为 splash_window
的 JFrame
,我将其用作我的应用程序的 启动画面,它首先显示。它有一个 progress bar
最初位于 0%
,我希望它更改为 5%
并在 用户可见后立即执行一些其他命令.
当我 运行 应用程序时,the progress bar
的进度是 已经 在 5%
之前 我的splash screen
变得可见,我可以在控制台中看到我的其他命令正在执行。
我的问题是:如何才能使命令在 splash screen
对用户 在屏幕上 ?
我的代码:
//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...
}
您可以向框架添加一个 window 侦听器,并在 window 打开时更新进度。这是一个例子:
splash_window.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
initProgress.setValue(getProgress(5));
initProgress.revalidate();
}
...
});