移动位置之前 JFrame 闪烁

发布于 2024-12-29 06:54:50 字数 240 浏览 5 评论 0原文

我注意到我的大多数挥杆 GUI 在居中之前都会闪烁。我制作的几乎所有 JFrame 都会发生这种情况,我想知道是否有解决方法。我通常调用 setVisible(true),然后调用 pack(),最后调用 setLocationRelativeTo(null)。这使得它出现在右上角,然后自行居中。我知道闪烁正在发生,因为每个方法调用都需要时间,但是是否有解决方法(使其变得漂亮和平滑)?

I noticed that most of my swing GUIs would flicker right before they center themselves. This happened with almost any JFrame that I have made, I was wondering if there is a workaround. I usually call setVisible(true), then pack(), followed by setLocationRelativeTo(null). This makes it appear in the top right and then center it self. I know that the flicker is happening because it's taking time for each method call, but is there a workaround for this (To make it nice and smooth)?

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

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

发布评论

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

评论(2

一百个冬季 2025-01-05 06:54:50

这个顺序(以及这些方法)更好:

  1. frame.pack();
  2. frame.setLocationByPlatform(true); // 优于居中
  3. frame.setVisible(true);

如果您设置了许多可见的框架,它们可能会出现像这样

当然,没有任何闪烁、摆动摇晃。 ;)


问题是 (setLocationRelativeTo(null)) 是 GUI 右上角居中,而不是整个 JFrame。

JFrame 的默认大小为 0x0,并且在 pack()setSize() 之前一直保持这种状态(但使用 pack( ) 正如您当前所做的那样)被调用。因此,如果您要求 0x0 组件在屏幕上居中,JRE 会将 0x0 大小组件的“中间像素”放置在屏幕的正中心。

或者,如果您先将其打包,它就会具有正确的尺寸,并且该方法将按预期工作。例如

frame.pack();
frame.setLocationRelativeTo(null); // show my splash screen!
frame.setVisible(true);

This order (and these methods) are better:

  1. frame.pack();
  2. frame.setLocationByPlatform(true); // superior to centering
  3. frame.setVisible(true);

If you set a number of frames visible doing that, they might appear something like this:

And of course, without any flicker, wobble or shake. ;)


The problem with that (setLocationRelativeTo(null)) is that the GUIs top right corner is centered and not the entire JFrame.

The defualt size of a JFrame is 0x0 and it stays that way until pack() or setSize() (but use pack() as you are doing currently) is called. So if you ask for a 0x0 component to be centered on screen, the JRE will place the 'middle pixel' of the 0x0 sized component at the exact center of the screen.

Alternately if you pack it first it has the correct size, and the method will work as expected. E.G.

frame.pack();
frame.setLocationRelativeTo(null); // show my splash screen!
frame.setVisible(true);
月隐月明月朦胧 2025-01-05 06:54:50

您应该首先执行setLocationRelativeTo(null),然后最后执行setVisible(true)

You should do first setLocationRelativeTo(null) and then setVisible(true) in the end.

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