Java - 解释gridbag权重以显示像素关系/公式/转换

时间:2011-07-25 00:04:16

标签: java swing math pixel gridbaglayout

我一直在玩GridBagLayout而我似乎无法弄清楚在限制条件下我将使用特定weight获得多少像素。

例如,假设我有一个1000像素contentPane,其中包含三个JPanel,其权重(例如,weighty)为0.1,0.1,0.8。

我的第一个假设是面板1和面板2各为100像素,面板3为800.事实证明它不是。

如何获得与加权相关的精确像素数?

This question提出了同样的问题,但答案虽然有帮助但却没有回答这个问题。 : - /

1 个答案:

答案 0 :(得分:2)

没关系,我想我找到了答案here

When more than one column has a non-zero weight, the excess space is 
distributed among the non-zero weight columns using the weight values. 
In particular, if the excess space is P pixels, and the column weights 
for column^i is weight^i, then column^i gets exactly 
(weight^i * P) / (sum-of-all-column-weights). For example, if column 1 
has weight 1 and column 2 has weight 2 and the excess space is 90 pixels, 
column 1 will get 30 extra pixels and column 2 will get 60 extra pixels. 
Rows with a non-zero weight behave in similar fashion.

显然问题是在细胞中添加了额外的空间。