带有GridBagLayout的嵌套JPanel [英] Nested JPanel with GridBagLayout

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

问题描述

我有一个JFrameGridBagLayout.为weightx和weighty值分配了不同的非零值,并且GridBagConstraints.fill = GridBagConstraints.BOTH.我在其中一个单元格中嵌套了一个JPanel,并为其提供了GridBagLayout.将组件添加到嵌套JPanel时,嵌套JPanel所在的单元格大小会在所有方面都增大,从而丢失了父级的布局.不使用插入和填充.
如何解决此问题? 这是GridBagConstraints值的示例:

I have a JFrame with GridBagLayout. weightx and weighty values are assigned different no-zero values and GridBagConstraints.fill = GridBagConstraints.BOTH. I nested a JPanel in one of the cells and gave it a GridBagLayout too. 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. Insets and padding are not used.
How can I fix this problem?Thanks. This is an example of the GridBagConstraints values:

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;

这是在将组件添加到嵌套JPanel之前的屏幕截图:

This is a screenshot before adding components to the nested JPanel:

以下是向嵌套JPanel添加组件后的屏幕截图:

This a screenshot after adding components to the nested JPanel:

推荐答案

这正是应该发生的情况.请说明您要寻找的行为.顺便说一句,J2SE可用的布局管理器并不理想.一生中做了大量的Swing工作,我强烈建议您查看JGoodies表单: http://www.jgoodies.com/freeware/forms/.查看白皮书,它比GridBag易于使用且直观得多.

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. 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/. Check out the whitepaper, it's easy to use and much more intuitive than GridBag.

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

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