I know this has already been answered but I felt that you deserved a look at how GridLayout Works. First off http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html and http://www.cs.ubc.ca/local/computing/software/jdk-1.5.0/docs/api/java/awt/GridBagConstraints.html help to decipher the long and cryptic looking method signatures.
There are three main parts for this Monopoly Board Example. There is the Setup of the Layout, the addition of the Large Middle Piece as a JPanels, and the addition of the outer squares as JPanels.
public class GridBagLayoutExample extends JFrame {
public static void main(String[] args) {
new GridBagLayoutExample().setVisible(true);
}
public GridBagLayoutExample() {
try {
//Setup the Layout
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
GridBagLayout thisLayout = new GridBagLayout();
thisLayout.rowWeights = new double[] { 0.2, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2 };
thisLayout.columnWeights = new double[] { 0.2, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2 };
getContentPane().setLayout(thisLayout);
//Default Grid values
int gridX = 0;
int gridY = 0;
//Add Panels for Each of the four sides
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 13; i++) {
JPanel tempPanel = new JPanel();
switch(j)
{
case 0://Top Spaces
gridX = i;
gridY = 0;
break;
case 1://Left Spaces
gridX = 0;
gridY = i;
break;
case 2://Right Spaces
gridX = 12;
gridY = i;
break;
case 3://Bottom Spaces
gridX = i;
gridY = 12;
break;
}
getContentPane().add(tempPanel,
new GridBagConstraints(gridX,// XGridSpot
gridY,// YGridSpot
1,// XGridSpaces
1,// YGridSpaces
0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH,//Fill
new Insets(0, 0, 0, 0), 0, 0));
tempPanel.setBorder(BorderFactory
.createLineBorder(Color.BLACK));
}
}
{// Main Inner Area Notice Starts at (1,1) and takes up 11x11
JPanel innerPanel = new JPanel();
getContentPane().add(
innerPanel,
new GridBagConstraints(1,
1,
11,
11,
0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
pack();
setSize(260, 260);
} catch (Exception e) {
e.printStackTrace();
}
}
}
From here, if you add a structure to hold the panels and then you can add buttons and whatever you want to each of the panels. Buttons would also work in place of the panels. This should compile with the right imports, so compile it and try it out.