Setting the Location of a Component Within the Cell of a GridBagLayout Using Anchors
By default, when the cell is larger than the preferred size of a
component, the component is centered within the cell. You can specify
other relative positions using the anchor constraint. For example,
specifying an anchor of NORTH causes the component to be
horizontally centered with the top edge of the component aligned with
the top edge of the cell. Specifying an anchor of NORTHEAST
causes the northeast corner of the component to coincide with the
northeast corner of the cell.
See Creating a GridBagLayout for an example on how to
use a gridbag layout with gridbag constraints.
GridBagConstraints gbc = new GridBagConstraints();
// These are the nine possible anchor values
gbc.anchor = GridBagConstraints.CENTER;
gbc.anchor = GridBagConstraints.NORTH;
gbc.anchor = GridBagConstraints.NORTHEAST;
gbc.anchor = GridBagConstraints.EAST;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.anchor = GridBagConstraints.WEST;
gbc.anchor = GridBagConstraints.NORTHWEST;
Post a comment