带有 GridBagLayout 的嵌套 JPanel

2024-01-03

我有一个JFrame with GridBagLayout。 Weightx 和 Weighty 值被分配不同的非零值并且 GridBagConstraints.fill = GridBagConstraints.BOTH。我在其中一个单元格中嵌套了一个 JPanel,并给了它一个 GridBagLayout。将组件添加到嵌套 JPanel 时,嵌套 JPanel 所在的单元格大小在所有侧面都会增长,从而丢失父级的布局。不使用插入和填充。
我该如何解决这个问题?谢谢。 这是 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:
enter image description here

This a screenshot after adding components to the nested JPanel:
enter image description here


这正是应该发生的事情。请解释您正在寻找的行为。顺便说一句,J2SE 提供的布局管理器不太理想。过去我做过大量的 Swing 工作,我强烈建议您查看 JGoodies 表单:http://www.jgoodies.com/freeware/forms/ http://www.jgoodies.com/freeware/forms/。查看白皮书,它很容易使用,并且比 GridBag 直观得多。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有 GridBagLayout 的嵌套 JPanel 的相关文章

随机推荐