2

Been struggling for this for some time. My method looks like this:

public Frame(){
            JFrame window = new JFrame();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setSize(800, 600);

            JPanel panel = new JPanel(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
              gbc.gridheight =3;
              gbc.gridwidth = 3;


            JButton upButt = new JButton();//Buttons.upButton();
              gbc.gridx = 1;
              gbc.gridy = 0;
              panel.add(upButt, gbc);

            JButton downButt = new JButton();
              gbc.gridx = 1;
              gbc.gridy = 2;
              panel.add(downButt, gbc);

            JButton leftButt = new JButton();//Buttons.leftButton();
              gbc.gridx=0;
              gbc.gridy = 1;
              panel.add(leftButt, gbc);

            JButton rightButt = new JButton();//Buttons.rightButton();
              gbc.gridx =2;
              gbc.gridy = 1;
              panel.add(rightButt, gbc);

            window.add(panel);
            window.setVisible(true);

           }

From my understanding - after reading and rereading the Java doc. - this should give me 4 buttons in a cross shape. This however isn't the case and the buttons are stacked on top of one another in the center of the window. What am I missing?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
spacitron
  • 449
  • 1
  • 5
  • 14

3 Answers3

4

Why are you using gridheight and gridwidth of 3? That's a bit strange to say the least.

For my money, I'd simplify things and use a simple GridLayout:

import java.awt.GridLayout;
import javax.swing.*;

public class Foo003 {
   public static void main(String[] args) {
      JButton upButton = new JButton("Up");
      JButton downButton = new JButton("Down");
      JButton leftButton = new JButton("Left");
      JButton rightButton = new JButton("Right");
      JComponent[][] components = { 
            { new JLabel(), upButton, new JLabel() },
            { leftButton, new JLabel(), rightButton },
            { new JLabel(), downButton, new JLabel() } };

      JPanel panel = new JPanel(new GridLayout(components.length,
            components[0].length, 8, 8));
      for (int i = 0; i < components.length; i++) {
         for (int j = 0; j < components[i].length; j++) {
            panel.add(components[i][j]);
         }
      }

      int eb = 15;
      panel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));

      JFrame frame = new JFrame("Grid e.g.");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

enter image description here

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • See [this answer](http://stackoverflow.com/questions/10861852/add-a-complex-image-in-the-panel-with-buttons-around-it-in-one-customized-user/10862262#10862262) for another example using a 3x3 `GridLayout` with alternating buttons and labels. – Andrew Thompson Aug 25 '12 at 00:03
  • "Why are you using gridheight and gridwidth of 3?" You are right. I deleted that and it works. I must have misunderstood the java docs. – spacitron Aug 25 '12 at 00:51
2

You mean something like:

Cross my buttons

I'd get rid of the gridwidth and gridheight

public class TestGridBagLayout extends JFrame {

    public TestGridBagLayout() {

        setTitle("Test");
        setLayout(new GridBagLayout());
        setSize(200, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        add(createButton(), gbc);

        gbc.gridy = 2;
        add(createButton(), gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        add(createButton(), gbc);

        gbc.gridx = 2;
        add(createButton(), gbc);

        setVisible(true);

    }

    protected JButton createButton() {

        return new JButton("+");

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new TestGridBagLayout();

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

When you use gridHeight and gridWidth what you saying is "This component should take up 3 rows and 3 columns".

From Java Doc:

gridheight
Specifies the number of cells in a column for the component's display area.

gridwidth
Specifies the number of cells in a row for the component's display area.

Here, the "component" is the component being added to the panel. Buttons in your case.

Do as the other posters say and remove the gridWidth and gridHeight lines and all should be fine.

km1
  • 2,383
  • 1
  • 22
  • 27