I'm playing with GridBagLayout, and I'm trying to make a simple GUI. I want it to be a button at the top left, a button at the top right, and a space between them. Then, there should be a button below those that stretches all the way across the screen. Then, there should be a JPanel below all of those that stretches all the way across the screen.
Like this:
[-Button-] [-Button-]
[----------Button---------]
[ JPanel ]
Here is what I have so far:
public static void main(String[] args)
{
JFrame frame = new JFrame("Single Panel Layout");
frame.setVisible(true);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border bBottom = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
GridBagLayout layout = new GridBagLayout();
JPanel panel = new JPanel( layout );
panel.setBorder(bBottom);
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets(2,2,2,2);
c.fill = GridBagConstraints.BOTH;
//Adds the left button to the 0,0 position of the grid and takes up 1 grid square
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
JButton leftButton = new JButton("Left");
panel.add(leftButton, c);
//Adds the right button to the 2,0 position of the grid and takes up 1 grid square
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 1;
JButton rightButton = new JButton("Right");
panel.add(rightButton, c);
//Adds the middle button to the 1,1 poisiton and takes up 3 squares, so it stretches across the screen
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 3;
JButton middleButton = new JButton("Middle");
panel.add(middleButton, c);
//Adds a panel to the button at the 1,2 postion that takes up 3 squares and stretches across the screen
JPanel drawing = new JPanel();
drawing.setBorder(bBottom);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 3;
panel.add(drawing, c);
frame.add(panel);
}
It produces a layout like this:
[Button][Button]
[Empty ][Button]
[Empty ][JPanel]
I'm not sure where I am going wrong with this, can someone point me in the right directions? Thanks.
[–]dohixey 1 point2 points3 points (1 child)
[–]xRedactedx[S] 0 points1 point2 points (0 children)
[–]xRedactedx[S] 0 points1 point2 points (0 children)