当某些组件不可见时使用frame.pack()?
我正在尝试创建一个简单的边框布局模板,并使用frame.pack() 来确定框架大小。我在中心区域有一个不可见的标签,并且 pack 方法似乎没有考虑到该标签,因此它没有显示。我知道它最初确实有效,因为框架最初的尺寸适合其他组件。 那么如何让框架考虑到隐形标签呢?仅当选择中心区域中的按钮时,标签才可见。 一切正常,除了您必须手动增加框架大小才能看到标签之外。
I'm trying to create a simple Border layout template and I'm using frame.pack() to determine frame size. I have an invisible label in the centre region and the pack method doesn't seem to be taking the label into account so it doesn't show. I know it does work initially because the frame is initially the right size for the other components.
So how can I get the frame to take the invisible label into account? The label is only visible when the button in the centre region is selected.
Everything works, apart from you have to manually increase the frame size to see the label.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这取决于包含标签的容器的布局管理器。一些布局管理器仅根据可见组件计算首选尺寸,而其他布局管理器则考虑不可见组件。例如,GroupLayout 具有
setHonorsVisibility
方法。无论如何,最简单的方法可能是在调用
pack
后使标签不可见。另请参阅 SetVisible(false) 更改我的组件的布局在我的面板中
I think that it depends on the layout manager of the container containing the label. Some layout managers compute the preferred size based on the visible components only, and others take invisible components into consideration. GroupLayout, for example, has the
setHonorsVisibility
method.Anyway, the easiest way is probably to make the label invisible after having called
pack
.See also SetVisible(false) changes the layout of my components within my Panel