我有一个名为 splash_window
的 JFrame
,我将其用作应用程序的启动画面,并且它首先显示。它有一个进度条
,最初位于0%
,我希望它更改为5%
并尽快执行一些其他命令它对用户可见。
当我运行应用程序时,进度条
的进度已经为5%
之前我的启动画面
变得可见,我可以在控制台中看到我的其他命令正在执行。
我的问题是:如何才能使用户在屏幕上看到闪屏
时立即执行命令?
我的代码:
//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...
}
最佳答案
您可以向框架添加窗口监听器,并在窗口打开时更新进度。这是一个例子:
splash_window.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
initProgress.setValue(getProgress(5));
initProgress.revalidate();
}
...
});
关于java - 仅当 JFrame 在屏幕上可见时才执行代码提前执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43227923/