在事件调度线程中显示 JWindow

发布于 2024-09-26 07:17:49 字数 340 浏览 3 评论 0 原文

我想做的是在我的程序加载某些内容时出现一个小的启动屏幕。这就是我所拥有的:

SplashScreen.showSplashScreen();
// Do stuff that takes time.
SplashScreen.hideSplashScreen();

所有 showSplashScreen() 方法所做的就是在屏幕中间创建一个新的 JWindow 并使其可见。

现在,此代码是从事件调度线程调用的,因此当调用 showSplashScreen() 方法时,直到线程完成后我才看到 JWindow,到那时,我不再需要该窗口了。在我等待时显示此启动屏幕的最佳方法是什么?

What I am trying to do is have a small splash screen appear while my program is loading something. This is what I have:

SplashScreen.showSplashScreen();
// Do stuff that takes time.
SplashScreen.hideSplashScreen();

All the showSplashScreen() method does is create a new JWindow in the middle of the screen and make it visible.

Now this code is called from the event dispatching thread, so when the showSplashScreen() method is called, I don't get to see the JWindow until the thread has finished, which by then, I don't need the window anymore. What would be the best way to go about showing this splash screen while I was waiting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2024-10-03 07:17:49

不确定这是否是“最好的方法”,但我之前使用过的一种机制是在 EDT 以外的线程上进行初始化,但使用 SwingUtilities.invokeAndWait。这样,即使您的初始化速度很快(如果您希望发生这种情况),您至少也能看到启动屏幕。

所以在你的初始化线程上,你可以:

SwingUtilities.invokeAndWait( /* Runnable to show splash screen */ );

// Do stuff that takes time.

SwingUtilities.invokeLater( /* Hide splash screen, display main GUI */ );

Not sure if this is the "best way", but a mechanism I've used before is to do your initialisation on a thread other than the EDT, but show your splash screen using SwingUtilities.invokeAndWait. That way, you'll at least get to see the splash screen even if your initialisation is quick (if that's what you want to happen).

So on your init thread, you go:

SwingUtilities.invokeAndWait( /* Runnable to show splash screen */ );

// Do stuff that takes time.

SwingUtilities.invokeLater( /* Hide splash screen, display main GUI */ );
红颜悴 2024-10-03 07:17:49

1.6 中引入了一个 java.awt.SplashScreen 类,尝试使用它吗?

There is a java.awt.SplashScreen class that was introduced in 1.6, tried using that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文