GridBagLayout的份量比对 [英] GridBagLayout weighty alignment

查看:157
本文介绍了GridBagLayout的份量比对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我dinamically产生这就需要使用相对大小的布局,我发现做到这一点,而无需使用外部Java布局库的唯一方法就是GridBagLayout中的的weightx和沉重。

在这一点上已经工作正是我需要一个小小的例外。当我有含有66.6%和33.3%的空间分布两个分别JPanels一列,然后使用空间的33.3%他们中的每一个中,第一列的33.3%是不一样的3 JPanels另一列第二的33.3%。我需要他们完全对齐。

不幸的是,这是我的第一篇,我无法发布任何照片,我希望我不会遇到麻烦做以下内容:

我想我知道问题是什么,每个JPanel的里面我有一个JLabel中以来的weightx和沉重的定义是权重来确定如何分配的列(的weightx)之间和行之间的空间(份量)我猜这两个33.3%S之间的区别在于,第二列包含一个额外的JLabel中的事实。

这个线程 StanislavL说,集装箱问孩子他们的preferred大小,所以,我想知道如果该解决方案是重写的JLabel的get preferredSize方法。
我不知道如何脏将做到这一点,我会AP preciate了很多你的建议来解决这个问题。

在此先感谢!

圣迭戈


解决方案

  

我想知道如果该解决方案是重写的JLabel的get preferredSize方法。


试试吧,除了我将覆盖板获得preferredSize()方法,因为它的面板尺寸为框架的增长,将调整/收缩。


  

我不知道如何脏将做到这一点。


压倒一切的是pferred使用一套preferredSize()方法$ P $。


  

我发现做到这一点,而无需使用外部Java布局库的唯一办法<​​/ P>

如果它使code更容易使用和理解为什么不使用外部库? 相对布局是专门为这个目的,所以你设计不必玩弄大小。

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.

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

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

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.

这篇关于GridBagLayout的份量比对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆