Java列问题中的GridBagLayout
我正在尝试使用GridBagLayout布局管理器来实现这一点: 然而,我目前得到的是: 问题是橙色和棕色/灰色面板应该占据第二列,但似乎只想在运行代码时占据第三列 我用于布局的代码:Java列问题中的GridBagLayout,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我正在尝试使用GridBagLayout布局管理器来实现这一点: 然而,我目前得到的是: 问题是橙色和棕色/灰色面板应该占据第二列,但似乎只想在运行代码时占据第三列 我用于布局的代码: Container contentPane = form.getContentPane(); contentPane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); J
Container contentPane = form.getContentPane();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel pnlGame = new JPanel();
pnlGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.85;
c.weighty = 0.65;
contentPane.add(pnlGame, c);
JPanel pnlBuy = new JPanel();
c.gridx = 2;
pnlBuy.setBackground(Color.blue); //temp
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.15;
c.weighty = 0.46;
contentPane.add(pnlBuy, c);
JPanel pnlUpgrade = new JPanel();
pnlUpgrade.setBackground(Color.yellow); //temp
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.15;
c.weighty = 0.19;
contentPane.add(pnlUpgrade, c);
JPanel pnlStats = new JPanel();
pnlStats.setBackground(Color.red); //temp
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.61;
c.weighty = 0.35;
contentPane.add(pnlStats, c);
JPanel pnlSpeed = new JPanel();
pnlSpeed.setBackground(Color.orange); //temp
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.04;
contentPane.add(pnlSpeed, c);
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 2;
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);
那么,我做错了什么?对不起,如果我的英语有点差劲,和/或我犯的错误是显而易见的。。。现在是早上20点到5点,我已经度过了漫长的一天。应该很快就会开始干了
更新:
看起来,如果我更改棕色/灰色面板的网格宽度,所有内容似乎都正确对齐,但最终在布局中出现了一个令人讨厌的间隙。在这里:
i、 imgur.com/6JUx2.png
以及小组守则(包括Kevin S建议的修正案):
那么,有没有什么我做错了,或者这只是GridBagLayout的一些怪异行为,我将不得不忍受
不幸的是,多亏了我的编辑,我已经失去了所有的嵌入,巴拉尔好心地放在那里。那么,恐怕我们回到了图片链接。现在看来,我不能发布超过两个超链接,所以在最后一个链接中,该链接已被删除,您需要复制并粘贴它
谢谢,Sam看来您需要修复pnlRounds JPanel:
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1; // NEEDS TO BE 1 (NOT 2)
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);
看起来你的问题是,当c.gridx=1
应该存在时,你的代码中有c.gridx=2
另外,正如旁注,底部面板的权重X不会增加到1.00,而是增加到0.99。只是觉得你应该知道 不完全是你的解决方案,更像是一个临时解决办法。如果添加此新面板
JPanel pnlTemp = new JPanel();
pnlTemp.setBackground(Color.pink); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlTemp, c);
使用灰色面板后,它将修复布局。不知道为什么,但在你(或其他人)能够找到正确的解决方案之前,这可能会有所帮助。这个新的粉红色面板不可见,但只是帮助修复了布局。下面是之后的布局
中间列中的所有组件至少也在另一列中。因此GridBagLayout将中间列的首选宽度计算为0,这就是您看到的效果
如果要确保中间的列具有更大的宽度,请在其中放置一些仅在此列中的组件。是否只希望布局正常 我做了一些修改
Container contentPane = form.getContentPane();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel pnlGame = new JPanel();
pnlGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1; // one row
c.gridheight = 1; // one column
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.40;
contentPane.add(pnlGame, c);
// Added two new components
// pnlgGame and pnlggGame
// Covers up 2nd column and rows 0 and 1
JPanel pnlgGame = new JPanel();
pnlgGame.setBackground(Color.green); //temp
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1; // one row
c.gridheight = 2; // two column
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.40;
contentPane.add(pnlgGame, c);
// Covers 2nd row and column 1
JPanel pnlggGame = new JPanel();
pnlggGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.20;
contentPane.add(pnlggGame, c);
JPanel pnlBuy = new JPanel();
pnlBuy.setBackground(Color.blue); //temp
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.40;
contentPane.add(pnlBuy, c);
JPanel pnlUpgrade = new JPanel();
pnlUpgrade.setBackground(Color.yellow); //temp
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.20;
contentPane.add(pnlUpgrade, c);
JPanel pnlSpeed = new JPanel();
pnlSpeed.setBackground(Color.BLUE); //temp
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.40;
c.weighty = 0.05;
contentPane.add(pnlSpeed, c);
JPanel pnlStats = new JPanel();
pnlStats.setBackground(Color.red); //temp
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.40;
contentPane.add(pnlStats, c);
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.40;
c.weighty = 0.35;
contentPane.add(pnlRounds, c);
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setVisible(true);
我已经做了您建议的两个修改(修正了列问题,并使底部权重x加起来为1),但输出仍然相同且不正确。对不起,谢谢你的帮助/谢谢目前我已经实施了这个变通方法,这样我就可以专注于程序的其余部分。对我来说,这看起来确实是GridBagLayout中的一个bug。事实上,Paŭlo Ebermann的回答解释了添加此临时面板如何修复布局。ŭlo感谢这一点,我现在理解了这个问题。
Container contentPane = form.getContentPane();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel pnlGame = new JPanel();
pnlGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1; // one row
c.gridheight = 1; // one column
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.40;
contentPane.add(pnlGame, c);
// Added two new components
// pnlgGame and pnlggGame
// Covers up 2nd column and rows 0 and 1
JPanel pnlgGame = new JPanel();
pnlgGame.setBackground(Color.green); //temp
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1; // one row
c.gridheight = 2; // two column
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.40;
contentPane.add(pnlgGame, c);
// Covers 2nd row and column 1
JPanel pnlggGame = new JPanel();
pnlggGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.20;
contentPane.add(pnlggGame, c);
JPanel pnlBuy = new JPanel();
pnlBuy.setBackground(Color.blue); //temp
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.40;
contentPane.add(pnlBuy, c);
JPanel pnlUpgrade = new JPanel();
pnlUpgrade.setBackground(Color.yellow); //temp
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.20;
contentPane.add(pnlUpgrade, c);
JPanel pnlSpeed = new JPanel();
pnlSpeed.setBackground(Color.BLUE); //temp
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.40;
c.weighty = 0.05;
contentPane.add(pnlSpeed, c);
JPanel pnlStats = new JPanel();
pnlStats.setBackground(Color.red); //temp
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.40;
contentPane.add(pnlStats, c);
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.40;
c.weighty = 0.35;
contentPane.add(pnlRounds, c);
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setVisible(true);