简体   繁体   English

GridBagLayout权重对齐

[英]GridBagLayout weighty alignment

I am dinamically generating a layout which requires to use relative sizes, the only way I found to do it without using an external Java layout library is GridBagLayout's weightx and weighty. 我正在动态地生成一个需要使用相对大小的布局,而我发现不使用外部Java布局库就这样做的唯一方法是GridBagLayout的weightx和weighty。

At this point it has been working exactly as I need with one small exception. 在这一点上,它已经完全按照我的需要工作了,只有一个小例外。 When I have one column containing two JPanels with a space distribution of 66.6% and 33.3% respectively and then another column with 3 JPanels using 33.3% of the space each one of them, the 33.3% of the first column is not the same as the 33.3% of the second. 当我有一个包含两个JPanels的列,它们的空间分布分别为66.6%和33.3%,然后另一个包含3个JPanels的列中的每个使用33.3%的空间时,第一列的33.3%与秒的33.3%。 I need them to be perfectly aligned. 我需要它们完全对齐。

Unfortunately this is my first post and I am not able to post any pictures, I hope I won't get in trouble for doing the following: 不幸的是,这是我的第一篇文章,并且我无法张贴任何图片,希望我不会因执行以下操作而遇到麻烦:

i.stack.imgur.com/avsuA.png

I think I know what the problem is, inside of each JPanel I have one JLabel and since weightx and weighty's definition is "Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty) " I guess the difference between the two 33.3%s is the fact that the second column contains one extra JLabel. 我想我知道问题出在哪里,在每个JPanel中我都有一个JLabel,因为weightx和weighty的定义是“权重用于确定如何在列(weightx)和行(weighty)之间分配空间 ”两个33.3%之间的差异是第二列包含一个额外的JLabel。

In this thread StanislavL says that the "container asks children for their preferred size", so, I am wondering if the solution is to override JLabel's getPreferredSize method. StanislavL在此线程中说“容器向孩子询问他们的首选大小”,因此,我想知道解决方案是否要覆盖JLabel的getPreferredSize方法。 I am not sure how "dirty" would be to do that, I would appreciate a lot your suggestions to solve this problem. 我不确定这样做会多么“肮脏”,非常感谢您提出的解决此问题的建议。

Thanks in advance! 提前致谢!

Diego 迭戈

I am wondering if the solution is to override JLabel's getPreferredSize method. 我想知道解决方案是否要覆盖JLabel的getPreferredSize方法。

try it, except I would override the panels getPreferredSize() method, since it the the panels size that will adjust as the frame grows/shrinks. 尝试一下,除非我会覆盖面板的getPreferredSize()方法,因为面板的大小会随着帧的增长/缩小而调整。

I am not sure how "dirty" would be to do that 我不确定这样做会有多“肮脏”

overriding is preferred to using the setPreferredSize() method. 覆盖优先于使用setPreferredSize()方法。

the only way I found to do it without using an external Java layout library 我发现不使用外部Java布局库就这样做的唯一方法

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. 相对布局是专门为此目的而设计的,因此您不必费心处理尺寸。

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

weighty won't "lock" each box to exactly 33% of the height; weighty不会将每个盒子“锁定”到正好33%的高度; 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). 因此,如果您需要它们完美setPreferredSize ,请使用setPreferredSize (而不是按照您的建议覆盖getPreferredSize )。

Use a single GridBagLayout for both columns rather than splitting apart. 两列都使用一个GridBagLayout而不是分开。 GridBagLayout allows you to do more than just an HTML table would, without having to hack your size and preferred size methods. GridBagLayout允许您执行比HTML表更多的事情,而不必修改您的大小和首选大小方法。 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 . 如您所见,将多个列添加到单个GridBagLayout毫无问题。

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

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