GridBagLayout in Java Column Issue

天大地大妈咪最大 提交于 2019-12-04 13:07:41

All the components in your middle column are at least in one other column as well. So the GridBagLayout calculates the preferred width of the middle column as 0, and this is the effect you are seeing.

If you want to make sure your middle column has more width, put some component there which is only in this column.

So it looks like you need to fix the 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);

Looks like your problem is that you had c.gridx = 2 in your code when c.gridx = 1 is what should really be there.

Also, just as a side note, the bottom panels' weightx don't add to 1.00, they add to .99. Just thought you should know.

Not exactly asolution for you but more like a temporary workaround. if you add this new panel

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

after you gray panel, it fixes the layout. Not sure why but maybe this will help until you are (or someone else is) able to figure out the correct solution. This new pink panel is not visible but just helps fix the layout somehow. Here's what the layout looks like after

Do you just want the layout to work?

I did some modification.

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