简体   繁体   English

Java GridBagLayout中的Weightx和Weighty

[英]Weightx and Weighty in Java GridBagLayout

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. 谢谢。

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. 如果Panel中的空间大于其中包含的组件的preferredDimension,则weightx和weighty用于将额外空间分配给各个组件。

use values from 0.0 to 1.0 (think of this a percentage). 使用0.0到1.0之间的值(想想这个百分比)。

  • weightx is horizontal spacing weightx是水平间距

  • 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). 桌面中最常见的情况是侧窗格保持固定大小(weightx / weighty = 0.0),中间窗格占用剩余空间(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 and weighty are used to determine how to distribute space among columns and among rows. weightxweighty用于确定如何在列之间和行之间分配空间。

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. 此值对于指定调整大小行为很重要。如果未指定weightx或weighty中的任何一个,则所有组件将在其容器的中心聚集在一起。 See the doc of GridBagLayout for more information. 有关更多信息,请参阅GridBagLayout的文档。

For each column, the weight is related to the highest weightx specified for a component within that column. 对于每列,权重与为该列中的组件指定的最高权重x相关。 Similarly, each row's weight is related to the highest weighty specified for a component within that row. 类似地,每行的权重与为该行内的组件指定的最高权重相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM