Reputation: 3080
So I used to be a great GUI programmer but it seems I have lost quite a bit of my GridBagLayout knowledge.
What I am trying to do is make a customized box with JPanels inside of it. I first used GridLayout, however from what I have researched there is really no way to customize the "% width/height" for each box, it simply evens them all out.
So my fist attempt resulted in this.
(GridLayout)
I continued looking around and I believe I need to use GridBagLayout. So I spent a good hour looking at examples and I feel like I am simply just "not getting it" for a lack of better words. I have tried implementing a variety of test codes for GridBagLaout but I can never get anything to fully work for what I need.
My question is, may someone please give me a skeleton setup of what I am looking for? I do not want the entire code, just the basic skeleton so that I can take a shot at figuring it out myself (that way I actually "get it").
So by skeleton, what I mean is make just one of the boxes for me and then please dumb down each step of what does what so that I can try to follow that same process for the rest of the boxes.
What I am looking to end up with is...
(My desired GridBagLayout)
As far as actually percents, I am looking at doing:
Width: 80% for green/red side, and 20% for blue side.
Height: 80% for red, and 20% for green/white.
I am currently finishing up a project today, but I hope that I can start working on this sometime this evening.
Thank you very much for all the future help! -Austin
Upvotes: 2
Views: 228
Reputation: 5415
Try this:
import java.awt.*;
import javax.swing.*;
public class GridBagDemo implements Runnable
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new GridBagDemo());
}
public void run()
{
JComponent redComp = new JPanel();
redComp.setBackground(Color.RED);
JComponent greenComp = new JPanel();
greenComp.setBackground(Color.GREEN);
JComponent blueComp = new JPanel();
blueComp.setBackground(Color.BLUE);
JComponent whiteComp = new JPanel();
whiteComp.setBackground(Color.WHITE);
GridBagConstraints gbc = new GridBagConstraints();
// we'll use this anchor/fill for all components
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
JPanel panel = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.weightx = 0.8; // use 80% of the overall width
gbc.weighty = 0.8; // use 80% of the overall height
panel.add(redComp, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.weightx = 0.2; // use 20% of the overall width
gbc.weighty = 1.0; // use 100% of the overall height
panel.add(blueComp, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.8; // use 80% of the width used by green/white comps
gbc.weighty = 0.2; // use 20% of the overall height
panel.add(greenComp, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.2; // use 20% of the width used by green/white comps
gbc.weighty = 0.2; // use 20% of the overall height
panel.add(whiteComp, gbc);
JFrame frame = new JFrame("GrigBag Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Upvotes: 2