Weightx and Weighty in Java GridBagLayout

 ̄綄美尐妖づ 提交于 2019-12-18 12:10:33

问题


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.


回答1:


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.




回答2:


weightx and weighty 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.



来源:https://stackoverflow.com/questions/5789513/weightx-and-weighty-in-java-gridbaglayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!