[英]Swing components twitch when resizing JFrame
I have an undecorated JFrame
which has a lot of components
inside it (such as JSplitPanes
, JPanels with GridBagLayouts, BoxLayouts, BorderLayouts
etc). 我有一个未经修饰的JFrame
,里面有很多components
(例如JSplitPanes
, JPanels with GridBagLayouts, BoxLayouts, BorderLayouts
)。 The code of building this JFrame
is 2500 lines length, so I wouldn't place it here, or it's simpled version, sorry. 构建此JFrame
的代码长度为2500行,因此,我不会在这里放置它,或者它是简单的版本,对不起。
When I drag the JFrame
by right
or bottom
side, it resizes OK, but when I drag it by left
or top
side, the components
inside the JFrame
are twitching a lot, so it looks very ugly. 当我将JFrame
拖动到right
或bottom
,它会调整OK的大小,但是当我将其拖动到left
或top
, JFrame
中的components
会抽动很多,因此看起来非常难看。
My question is: Why does it happen? 我的问题是:为什么会发生? How can I prevent it (if I can)? 如何预防(如果可以)? Does anyone fix that in own practice? 有人在自己的实践中解决此问题吗?
UPD: I've written my own resizer for JFrame
. UPD:我已经为JFrame
编写了自己的大小调整器。 It works OK for other windows, which have less amount of components. 对于其他具有较少组件的窗口,它可以正常工作。
Resizing the frame implicitly validates the enclosed Container
, which causes doLayout()
to be invoked on the affected components each time the size changes. 调整框架的大小会隐式验证封闭的Container
doLayout()
,这会导致每次更改大小时在受影响的组件上调用doLayout()
。 You can see a similar effect in repeated calls to paintComponent()
when resizing this AnimationTest
. 调整此AnimationTest
大小时,您在重复调用paintComponent()
时会看到类似的效果。 As you observe, using thousands of components scales poorly. 如您所见,使用数千个组件的伸缩性很差。 As an alternative, leverage the flyweight pattern to render only visible cells, as is done in JTable
, JTree
, JList
, etc. If you can't use one of these, CellRendererPane
, seen here , may help. 作为替代方案,可以利用flyweight模式仅呈现可见单元格,如JTable
, JTree
, JList
等所示。如果您不能使用其中之一,则CellRendererPane
(可能在此处看到)可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.