简体   繁体   English

嵌套JPanel与GridBagLayout

[英]Nested JPanel with GridBagLayout

I have a JFrame with GridBagLayout . 我有一个带GridBagLayoutJFrame weightx and weighty values are assigned different no-zero values and GridBagConstraints.fill = GridBagConstraints.BOTH. weightx和weighty值被赋予不同的no-zero值,GridBagConstraints.fill = GridBagConstraints.BOTH。 I nested a JPanel in one of the cells and gave it a GridBagLayout too. 我在其中一个单元格中嵌套了一个JPanel,并给它一个GridBagLayout。 When adding components to the nested JPanel, the cell size where the nested JPanel resides grows on all sides missing up the parent's layout. 将组件添加到嵌套的JPanel时,嵌套的JPanel所在的单元格大小会在所有方面增长,从而缺少父级的布局。 Insets and padding are not used. 不使用插入和填充。
How can I fix this problem?Thanks. 我该如何解决这个问题?谢谢。 This is an example of the GridBagConstraints values: 这是GridBagConstraints值的示例:

GridBagConstraints treePanCon = new GridBagConstraints();
treePan.setLayout(treePanGridbag);
treePanCon.fill = GridBagConstraints.BOTH;
treePanCon.weightx = 0.5;
treePanCon.weighty = 1;
treePanCon.gridx = 0;
treePanCon.gridy = 0;
treePanCon.gridwidth = 1;
treePanCon.gridheight = 1;

This is a screenshot before adding components to the nested JPanel: 这是在向嵌套JPanel添加组件之前的屏幕截图:
在此输入图像描述

This a screenshot after adding components to the nested JPanel: 这是将组件添加到嵌套JPanel后的屏幕截图:
在此输入图像描述

That's exactly what is supposed to happen. 这正是应该发生的事情。 Please explain the behavior you're looking for. 请解释您正在寻找的行为。 As an aside the layout managers available with J2SE are less than ideal. 另外,J2SE提供的布局管理器并不理想。 Having done a large amount of Swing work in a past life I would highly recommend checking out JGoodies forms: http://www.jgoodies.com/freeware/forms/ . 在过去的生活中完成了大量的Swing工作,我强烈建议您查看JGoodies表格: http ://www.jgoodies.com/freeware/forms/。 Check out the whitepaper, it's easy to use and much more intuitive than GridBag. 查看白皮书,它易于使用,比GridBag更直观。

This is probably of absolutely no use to you at this late date, but... 在这个较晚的日期,这对你来说可能毫无用处,但......

It looks like you probably used BOTH when laying out the two new buttons. 看起来你在布置两个新按钮时可能会使用BOTH。

If the preferredSize.width on the tree is the width you want for the inner panel, 如果树上的preferredSize.width是内部面板所需的宽度,
and you wanted the two new buttons to each be half the width of the tree, 你想要两个新的按钮,每个按钮的宽度是树的一半,

set their preferredSize.width to half the preferredSize.width of the tree, 将preferredSize.width设置为树的preferredSize.width的一半,
and use NONE on the two buttons. 并在两个按钮上使用NONE。

Use VERTICAL on the tree in the inner panel. 在内部面板的树上使用VERTICAL。 Use VERTICAL on the inner panel in the outer panel. 在外部面板的内部面板上使用VERTICAL。

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

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