我是 GridBagLayout
主题的新手,我无法理解约束 weight
和 fill
之间的确切区别。
我可以在不分配权重
的情况下填充
吗?
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
我的问题是,如果这是真的,那么为什么组件之间没有空间并且它们看起来是相连的?
最佳答案
weightx/y
指定给定单元格将具有的“权重”量。重量越大,电池占据的空间就越大。可用空间量是布置完所有其他组件后剩余的剩余空间。
fill
确定组件如何扩展以占据给定单元格的可用空间。也就是说,如果同一列中有两个组件,则可以使用将 fill
设置为 HORIZONTAL
或 BOTH
来允许组件)来“扩展”以填充列的可用空间,例如...
这显然也适用于同一行中的组件并使用VERTICAL
或BOTH
Short
按钮设置为 fill
BOTH
及其 weightx
和 weighty 的示例
设置为 1
。短按钮想要占据容器的所有剩余空间。
确保您已通读 How to Use GridBagLayout了解更多详情
My question is that, if this is true then why there is no space between the components and they looked attached?
GridBagLayout
默认情况下不会在组件之间添加任何间距,但您可以通过 insets
属性指定间距
关于java - GridBagLayout 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24505482/