移动位置之前 JFrame 闪烁
我注意到我的大多数挥杆 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个顺序(以及这些方法)更好:
frame.pack();
frame.setLocationByPlatform(true); // 优于居中
frame.setVisible(true);
如果您设置了许多可见的框架,它们可能会出现像这样:
当然,没有任何闪烁、摆动或摇晃。 ;)
JFrame
的默认大小为 0x0,并且在pack()
或setSize()
之前一直保持这种状态(但使用pack( )
正如您当前所做的那样)被调用。因此,如果您要求 0x0 组件在屏幕上居中,JRE 会将 0x0 大小组件的“中间像素”放置在屏幕的正中心。或者,如果您先将其打包,它就会具有正确的尺寸,并且该方法将按预期工作。例如
This order (and these methods) are better:
frame.pack();
frame.setLocationByPlatform(true); // superior to centering
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 defualt size of a
JFrame
is 0x0 and it stays that way untilpack()
orsetSize()
(but usepack()
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.
您应该首先执行
setLocationRelativeTo(null)
,然后最后执行setVisible(true)
。You should do first
setLocationRelativeTo(null)
and thensetVisible(true)
in the end.