GridBagLayout weighty alignment

╄→尐↘猪︶ㄣ 提交于 2019-12-06 13:30:21

I am wondering if the solution is to override JLabel's getPreferredSize method.

try it, except I would override the panels getPreferredSize() method, since it the the panels size that will adjust as the frame grows/shrinks.

I am not sure how "dirty" would be to do that

overriding is preferred to using the setPreferredSize() method.

the only way I found to do it without using an external Java layout library

Why not use an external library if it make the code easier to use and understand? Relative Layout was specifically designed for this purpose so you don't have to play around with sizes.

asaini007

The accepted answer to this question states:

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.

weighty won't "lock" each box to exactly 33% of the height; it doesn't distribute all of the space, but only the extra space.

So, if you need them to line up perfectly, use setPreferredSize (rather than of overriding getPreferredSize as you suggested).

Use a single GridBagLayout for both columns rather than splitting apart. GridBagLayout allows you to do more than just an HTML table would, without having to hack your size and preferred size methods. You can do something as follows in your frame initializer:

getContentPane().setLayout(new GridBagLayout());
GridBagConstraints constraints = null;
Insets insets = new Insets(0, 0, 0, 0);
...
// upper left hand corner, 1 column wide, 2 rows high, make the column take up half of the total width, the row(s) take up 0.66 of the total height
constraints = new GridBagConstraints(0, 0, 1, 2, 0.5, 0.66, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0);
getContentPane().add(upperLeftPanel, constraints);

// lower left hand corner, 1 column wide, 1 row high, make the column take up half of the total width, the row take up 0.33 of the total height
constraints = new GridBagConstraints(0, 1, 1, 1, 0.5, 0.33, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0);
getContentPane().add(lowerLeftPanel, constraints);

// upper right hand corner, 1 column wide, 1 row high, make the column take up half of the total width, the row take up 0.33 of the total height
constraints = new GridBagConstraints(1, 0, 1, 1, 0.5, 0.33, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0);
getContentPane().add(upperRightPanel, constraints);

// center right hand side, 1 column wide, 1 row high, make the column take up half of the total width, the row take up 0.33 of the total height
constraints = new GridBagConstraints(1, 1, 1, 1, 0.5, 0.33, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0);
getContentPane().add(centerRightPanel, constraints);

// lower right hand corner, 1 column wide, 1 row high, make the column take up half of the total width, the row take up 0.33 of the total height
constraints = new GridBagConstraints(1, 2, 1, 1, 0.5, 0.33, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0);
getContentPane().add(bottomRightPanel, constraints);

The various properties of the constraints, as well as the container where you will really add the panels is entirely up to you. You can change the same constraints object or create a new one every time. I have see both used, but tend to favor the latter myself. As you can see, there is no problem adding multiple columns to a single GridBagLayout.

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