Java布局-为不可见元素留出一定的空间
我正在为自己制作一个 Swing 应用程序,并尝试了几种不同的布局。在我尝试过的所有布局(BoxLayout、FlowLayout、BorderLayout、GroupLayout)中,我遇到的一个问题是我想隐藏我的一个元素,但我想留下一个确切大小的空白空间在它的地方。目前,当元素消失时,或者如果我将元素设置为在启动时不可见,那么整个窗口会变小,或者会发生一些事情,例如文本字段将扩展到可笑的大小。
指定元素的大小似乎根本没有效果。
有没有办法让一个不可见的元素仍然占据布局中的空间?
为什么我的元素没有指定我指定的尺寸?
感谢任何指点。
非常感谢
I'm making a swing application for myself and have been trying out a few different layouts. One thing that I'm having an issue with in all the layouts I've tried (BoxLayout, FlowLayout, BorderLayout, GroupLayout) is that I want to hide one of my elements, but I want to leave a blank space of the exact size in it's place. At the moment, when the element disappears, or if I set the element as not visible on startup, then either the whole window is smaller, or something will happen like a textfield will expand to a rediculous size.
Specifying the sizes of the elements seem to have no effect at all.
Is there a way I can have an invisible element that still takes up it's space in the layout?
Why are my elements not being given the sizes that I specify?
Grateful for any pointers.
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
Box.createRigidArea
。描述很简单:这将创建一个可在任何布局中使用的
组件
。Try
Box.createRigidArea
. The description is simple enough:This creates a
Component
that can be used in any layout.“想要隐藏我的一个元素,但我想在它的位置留下精确大小的空白空间”的唯一完全安全的方法是使用 LayoutManager,在其布局过程中包含不可见的组件。如果没有一个核心实现可以做到这一点,请使用第三方管理器,例如 fi MigLayout。
注意:Box.RigidArea 具有固定大小 - 因此对于特定布局状态下的占位符来说足够好,但如果 fi 父级大小发生变化,则需要手动调整。
The only completely safe way of "want to hide one of my elements, but I want to leave a blank space of the exact size in it's place" is a LayoutManager that includes invisible components in its layout process. If none of the core implementations does it, use a third-party manager like f.i. MigLayout.
Note: Box.RigidArea has a fixed size - so its good enough for a placeholder at a specific layout state, but needs to be manually adjusted if f.i. parent size changes.
尝试将
component.setVisible(false);
放在frame.pack();
之后Try placing
component.setVisible(false);
afterframe.pack();