Java – gridbaglayout: how to prevent invisible components from crashing

When making the components of the layout managed by gridbaglayout invisible, the arrangement of other components in the layout will change continuously Gridbaglayout appears to "collapse" cells with invisible components, causing other components to change position It's a very annoying behavior, and I've been looking for a way to stop it

One method I have tried is to add a spacer in the same cell. Its fixed size is equal to the preferred size of components that may be hidden. However, if you want to change the preferred size of components, you will encounter a lot of trouble to keep them synchronized Is there a better way?

In fact, if gridbaglayout really completely ignores cells with invisible components, it won't be so bad, but it still considers cell illustrations, so if you have a layout of input fields and use illustrations to neatly arrange the gap between them, then hide the middle component and remove it from the layout but leave a gap, This usually means that the surrounding components flow together, but there is an ugly double wide gap between them

Solution

How about using JPanel (or @ r_649_2419 @) as a placeholder? Put JPanel in each cell and add components to the panel If you can't see the component, the panel should still be there

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>