Java GridBagLayout 中的 Weightx 和 Weighty
我在理解这两个属性时遇到一些困难。我应该如何赋予组件权重?这些数字是如何计算的?我尝试阅读网上的几篇文章,但我不明白。
谢谢。
I have some trouble understanding these two properties. How should I give weight to components? How are these numbers calculated? I have tried to read several articles on the web but I do not understand it.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果Panel内的空间大于其中包含的组件的preferredDimension,则使用weightx和weighty将额外的空间分配给各个组件。
使用 0.0 到 1.0 之间的值(将其视为百分比)。
weightx 是水平间距
weighty 是垂直间距
桌面中最常见的情况是侧窗格保持固定大小(weightx/weighty = 0.0),中心窗格占据剩余空间(weightx/weighty = 1.0)。但是,使用变体,您可以完全控制应用程序如何随着框架大小的变化调整各个组件的大小。
If the space within a Panel is greater than the preferredDimension of the components contained within, the weightx and weighty is used to distribute the extra space to the individual components.
use values from 0.0 to 1.0 (think of this a percentage).
weightx is horizontal spacing
weighty is vertical spacing
The most common scenario in desktops is that the side panes stay a fixed size (weightx/weighty = 0.0) and the center pane takes up the remaining space (weightx/weighty = 1.0). however, using variations, you can have complete control of how your application resizes the individual components as the Frame size changes.
weightx
和weighty
用于确定如何在列之间和行之间分配空间。该值对于指定调整大小行为非常重要。如果您不指定weightx 或weighty 中的任何一个,则所有组件将聚集在其容器的中心。
有关更多信息,请参阅 GridBagLayout 的文档信息。
对于每一列,权重与为该列内的组件指定的最高权重x相关。类似地,每行的权重与为该行内的组件指定的最高权重相关。
weightx
andweighty
are used to determine how to distribute space among columns and among rows.This values are important for specifying resizing behavior.If you do not specify any of weightx or weighty, all the components will clump together in the center of their container.
See the doc of GridBagLayout for more information.
For each column, the weight is related to the highest weightx specified for a component within that column. Similarly, each row's weight is related to the highest weighty specified for a component within that row.