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

发布于 2024-11-26 01:22:15 字数 389 浏览 2 评论 0原文

我一直在使用 GridBagLayout ,但我似乎无法弄清楚在约束中使用特定的权重会获得多少像素。

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

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

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

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

I've been playing around with GridBagLayout and I can't seem to figure out how many pixels I'm going to get with a particular weighting in the constraints.

For example, say I have a 1000 pixel contentPane with three JPanels in it, with weights (say, weighty) of 0.1, 0.1, 0.8.

My first hypothesis was that panel 1 and 2 would be 100 pixels each, and panel 3 would be 800. Turns out it wasn't.

How can I get an exact number of pixels to expect with respect to the weighting?

This question asks sort of the same thing, but the answers, while helpful, don't answer the question. :- /

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自控 2024-12-03 01:22:15

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

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.

显然问题是额外的空间被添加到单元格中。

Never mind, I think I found the answer 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.

Apparently the problem is extra space being added to the cells.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文