java - GridBagLayout 如何工作?

标签 java swing layout layout-manager gridbaglayout

我是 GridBagLayout 主题的新手,我无法理解约束 weightfill 之间的确切区别。

我可以在不分配权重的情况下填充吗?

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 设置为 HORIZONTALBOTH 来允许组件)来“扩展”以填充列的可用空间,例如...

Short Long

这显然也适用于同一行中的组件并使用VERTICALBOTH

Short 按钮设置为 fill BOTH 及其 weightxweighty 的示例 设置为 1。短按钮想要占据容器的所有剩余空间。

Weighted

确保您已通读 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/

相关文章:

JavaFX 仅展开包含特定子级的 TreeView 父级

java - Android Tess-Two OCR 不可映射字符 'fi'

Windows Server 2003 中的 Java swing 错误

python - 绘图中缺少显示的标题

html - 两列并排

java - 排序集变换

java - 我让 'this' 引用逃逸了吗? (构造函数实例化中的内部类,无事件监听器,无线程)

java - 1.JFrame 调用 2. JFrame - wait() => 都被阻止?

java - 画圈 : separation of code into classes

android - 将元素动态加载到 ScrollView