1

我一直在玩GridBagLayout,我似乎无法弄清楚weight在约束中的特定 ing 中我将获得多少像素。

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

我的第一个假设是面板 1 和 2 各为 100 像素,面板 3 为 800 像素。事实证明并非如此。

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

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

4

1 回答 1

2

没关系,我想我在这里找到了答案:

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.

显然问题是向单元格添加了额外的空间。

于 2011-07-25T00:09:26.217 回答