GridBagLayout
I'm trying to use the GridBagLayout for a panel i'm designing. The problem is that when I compile and run the app, the JButton gets placed directly in the middle of the panel, instead of at 0,0 like I coded it to. Can someone tell me what's wrong with this code?
import javax.swing.*;
import java.awt.*;
publicclass Test
{
publicstaticvoid main(String[] args)
{
MyFrame x =new MyFrame();
}
}
class MyFrameextends JFrame
{
public MyFrame()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(200,200,200,200);
add(new MyPanel());
setVisible(true);
}
}
class MyPanelextends JPanel
{
public MyPanel()
{
setLayout(new GridBagLayout());
JButton button =new JButton("Click");
add(button, getConstraints(0,0,3,3, GridBagConstraints.EAST));
}
private GridBagConstraints getConstraints(int gridx,
int gridy,int gridWidth,int gridHeight,int anchor)
{
GridBagConstraints c =new GridBagConstraints();
c.insets =new Insets(5, 5, 5, 5);
c.ipadx = 0;
c.ipady = 0;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
c.anchor = anchor;
return c;
}//end getConstraints
}
[2673 byte] By [
kwiknessa] at [2007-11-27 5:00:59]

# 3
Ok, now I've updated my code, but now, the components are being placed directly on top of one another. Why is this happening? The new code in my panel constructor is:
public MyPanel()
{
setLayout(new GridBagLayout());
JButton shuffle = new JButton("Shuffle");
JButton deal = new JButton("Deal");
add(shuffle, getConstraints(0,0,0,0, GridBagConstraints.WEST));
add(deal, getConstraints(1,0,0,0, GridBagConstraints.WEST));
}
# 6
Here, try this code:
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setBounds(200,200,200,200);
//getContentPane().add(new MyPanel());
getContentPane().setLayout(new GridBagLayout());
JButton button = new JButton("Click");
Insets insets = new Insets(0,0,0,0);
GridBagConstraints constraints = new GridBagConstraints(
0, 0, // gridx, gridy
1, 1, // gridwidth, gridheight
0.5, 0.5, // weightx, weighty
GridBagConstraints.NORTHWEST, //CENTER, // anchor
GridBagConstraints.BOTH, // fill
// GridBagConstraints.NONE, // fill
// GridBagConstraints.VERTICAL, // fill
// GridBagConstraints.HORIZONTAL, // fill
insets, // insets
0, // ipadx
0); // ipady
getContentPane().add(button, constraints);
JButton button2 = new JButton("Click");
constraints = new GridBagConstraints(
1, 0, // gridx, gridy
1, 2, // gridwidth, gridheight
0.25, 0.25, // weightx, weighty
GridBagConstraints.NORTHWEST, //CENTER, // anchor
GridBagConstraints.BOTH, // fill
// GridBagConstraints.NONE, // fill
// GridBagConstraints.VERTICAL, // fill
// GridBagConstraints.HORIZONTAL, // fill
insets, // insets
0, // ipadx
0); // ipady
getContentPane().add(button2, constraints);
JButton button3 = new JButton("Click");
constraints = new GridBagConstraints(
0, 1, // gridx, gridy
1, 1, // gridwidth, gridheight
0.25, 0.25, // weightx, weighty
GridBagConstraints.NORTHWEST, //CENTER, // anchor
GridBagConstraints.BOTH, // fill
// GridBagConstraints.NONE, // fill
// GridBagConstraints.VERTICAL, // fill
// GridBagConstraints.HORIZONTAL, // fill
insets, // insets
0, // ipadx
0); // ipady
getContentPane().add(button3, constraints);
pack();
setVisible(true);
Your problem was cause by a combination of things ... using setBounds instead of pack, not using weighty and weightx, not positioning them properly on the grid ... you need to change the constrainsts for each Component.
# 8
In your example - try this:
public MyPanel()
{
setLayout(new GridBagLayout());
JButton button = new JButton("Click");
JButton button2 = new JButton("Click Again");
add(button, getConstraints(0,0,1,1, GridBagConstraints.CENTER));
add(button2, getConstraints(1,0,2,1, GridBagConstraints.CENTER));
}