I'm new to GridBagLayout but I tried to use the standard convention that I could find which was to draw out my idea on a piece of grid paper and then try and translate the grid values into gridbag...
my goal is to make the layout like you see below:
it currently looks like this:
any idea's as to why?
the exact dimensions i'm looking for if you think of a grid with the upper left hand corner being 0,0 for
- the red in panel in the goal picture: start at column 0, span 10 columns, with height 1 row
- for the black panel: start at column 0, row 1, span 10 columns, with height 20 rows
- for the blue panel: start at column 0, row 21, span 10 columns, with height 1
- for the green column: start at column 10, row 0, span 16 columns, with height 7
- for the purple column: start at column 10, row 7, span 16 columns, with height 16
here is my source code:
GBC is a helper class that extends GridBagConstraints, the constructor used is
GBC(int startingX,int startingY,int width,int height)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Demo extends JApplet
{
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
public void init()
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
initComponents();
}
});
}
public void initComponents()
{
//set the layout of the content pane to gridbag layout
GridBagLayout gridBag = new GridBagLayout();
getContentPane().setLayout(gridBag);
Rectangle rect = getContentPane().getBounds();
panel1.setBackground(Color.green);
panel2.setBackground(Color.black);
panel3.setBackground(Color.red);
panel4.setBackground(Color.orange);
panel5.setBackground(Color.yellow);
add(panel4, new GBC(10, 0, 16, 7).setFill(GBC.BOTH).setWeight(1.0, 1.0));
add(panel1, new GBC(0, 0, 10, 1).setFill(GBC.BOTH).setWeight(1.0, 1.0));
add(panel3, new GBC(0, 21, 10, 1).setFill(GBC.BOTH).setWeight(1.0, 1.0));
add(panel2, new GBC(0, 1, 10, 20).setFill(GBC.BOTH).setWeight(1.0, 1.0));
add(panel5, new GBC(10, 7, 16, 16).setFill(GBC.BOTH).setWeight(1.0, 1.0));
}
}
any help would be appreciated (but please explain your logic)
See Question&Answers more detail:os