Nested JPanel with GridBagLayout

左心房为你撑大大i 提交于 2019-12-23 17:35:05

问题


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;

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

This a screenshot after adding components to the nested JPanel:


回答1:


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.




回答2:


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.

If the preferredSize.width on the tree is the width you want for the inner panel,
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,
and use NONE on the two buttons.

Use VERTICAL on the tree in the inner panel. Use VERTICAL on the inner panel in the outer panel.



来源:https://stackoverflow.com/questions/7236551/nested-jpanel-with-gridbaglayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!