2011-03-27 53 views
3

我试图使用到的GridBagLayout布局管理器来实现这一目标:的GridBagLayout在Java中列问题

enter image description here

然而,什么我目前得到的是这样的:

enter image description here

问题是橙色和棕色/灰色面板应该占据第二列,但似乎只想在运行代码时占用第三列。

,我使用的布局代码:

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点,而且我度过了漫长的一天。很可能很快就会碰到干草。

UPDATE:

看来,如果我改变了棕/灰面板的gridwidth,似乎一切都正确对齐,但我最终在我的布局讨厌的差距。在这里:

i.imgur.com/6JUx2.png

而对于面板的代码(包括凯文小号建议修订):

JPanel pnlRounds = new JPanel(); 
pnlRounds.setBackground(Color.gray); //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(pnlRounds, c); 

那么,有没有什么,我做错了,还是这只是GridBagLayout的一些奇怪的行为,我将不得不忍受?

不幸的是,多亏了我的编辑,我已经失去了Bala R善意地放在那里的所有嵌入。所以,我们回到了图像的链接上,恐怕。而现在看来,我不能发布两个以上的超链接,那么该链接在最近一个被杀害,你需要复制和粘贴。

谢谢,山姆

回答

1

所有在中间栏的部件至少在一个其他列好。因此,GridBagLayout将中间列的首选宽度计算为0,这就是您所看到的效果。

如果你想确保你的中间列有更多的宽度,把一些只在这个列中的组件。

+0

ŭlo谢谢你,我现在明白了这个问题。 – 2011-03-27 14:54:40

0

所以它看起来像你需要修复的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 = 2c.gridx = 1是什么确实应该存在。另外,就像旁注一样,底部面板的weightx不会增加到1.00,它们会增加到0.99。只是觉得你应该知道。

+0

我已经提出了两个修改建议(固定列问题,并使bottom weightx加起来为1),但输出仍然是相同和不正确的。 对不起,感谢您的帮助。 :/ – 2011-03-27 12:12:53

0

并不完全是为了解决您的问题,而更像是临时的解决方法。如果你添加这个新的面板

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); 

灰色面板后,它修复了布局。不知道为什么,但也许这将有助于直到你(或其他人)能够找出正确的解决方案。这个新的粉色面板不可见,但只是有助于以某种方式修复布局。这里的布局是什么样后

enter image description here

+0

谢谢!我暂时实施了这个解决方法,以便我可以专注于程序的其余部分。对我来说,这确实看起来是GridBagLayout中的一个错误。 – 2011-03-27 14:34:12

+0

其实Pa?lo Ebermann的答案解释了如何添加这个临时面板修复布局。 – 2011-03-27 14:35:37

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);