一列布局不使用窗口的整个宽度
我想创建一个布局:2 行,1 列。第一行应占据窗口高度的 70%,第二行应占据窗口高度的 30%。我通过使用 GridBagConstraints
的 weighty
属性来实现此目的。
但是,我对组件的宽度有问题,因为当我调整应用程序窗口大小时,组件保持在中心,其宽度是恒定的,并且组件的左侧和右侧有空格(即使我设置了 填充
到BOTH
)。当我更改窗口的高度时,不会出现此问题(组件可以很好地调整大小并填充窗口的整个高度)。
以下是我的限制:
firstConstraints.gridx = 0;
firstConstraints.gridy = 0;
firstConstraints.weighty = 0.7;
firstConstraints.fill = GridBagConstraints.BOTH;
secondConstraints.gridx = 0;
secondConstraints.gridy = 1;
secondConstraints.weighty = 0.3;
secondConstraints.fill = GridBagConstraints.BOTH;
I would like to create a layout: 2 rows, 1 column. 1st row should occupy 70% height of the window and 2nd row 30% of the window. I achieve this by using weighty
attribute of GridBagConstraints
.
However I have problem with the width of my component, because when I resize application window the component remain in the center, its width is constant and I get a white spaces in the left and right of the component (even if I set fill
to BOTH
). This problem does not occur when I change the height of the window (components resize very well and fill full height of the window).
Below my constraints:
firstConstraints.gridx = 0;
firstConstraints.gridy = 0;
firstConstraints.weighty = 0.7;
firstConstraints.fill = GridBagConstraints.BOTH;
secondConstraints.gridx = 0;
secondConstraints.gridy = 1;
secondConstraints.weighty = 0.3;
secondConstraints.fill = GridBagConstraints.BOTH;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您还需要:
请参阅 Swing 教程中关于 如何使用一个 GrigBagLayout 讨论了weightx,权重约束。
I think you also need:
See the secton from the Swing tutorial on How to Use a GrigBagLayout that talks about the weightx, weighty constraints.
简单的例子
simple example