Package java.awt |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
public class java.awt.GridBagLayout extends java.lang.Object implements java.awt.LayoutManager { // Fields protected final static int MAXGRIDSIZE; protected final static int MINSIZE; // Constructors public GridBagLayout(); // Methods public void addLayoutComponent(String name, Component comp); public GridBagConstraints getConstraints(Component comp); public void layoutContainer(Container target); protected GridBagConstraints lookupConstraints(Component comp); public Dimension minimumLayoutSize(Container target); public Dimension preferredLayoutSize(Container target); public void removeLayoutComponent(Component comp); public void setConstraints(Component comp, GridBagConstraints constraints); public String toString(); }
The grid bag layout manager is a flexible layout manager that aligns components horizontally and vertically, without requiring that the components be the same size.
Each grid bag layout manager uses a rectangular grid of cells, with each component occupying one or more cells (called its display area). Each component in a grid bag layout is associated with a set of constraints contained within a GridBag-Constraints instance that specifies how the component is to be laid out within its display area.
The manner in which the grid bag layout manager places a set of components depends on each component's constraints and its minimum size, as well as the preferred size of the components' container.
To use a grid bag layout effectively, one or more components must have a customized GridBagConstraints objects created for it.
The fields of the GridBagConstraints object are described more fully in Class GridBagConstraints.
The following figure shows ten components (all buttons) managed by a grid bag layout manager:
Each of the ten components has the fill field of their constraint set to GridBagConstraints.BOTH. In addition, the buttons have the following non-default constraints:
In addition, the components have the following non-default constraints:
Here is the code that implements the example shown above:
import java.awt.*; import java.util.*; import java.applet.Applet; public class GridBagEx1 extends Applet { protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) { Button button = new Button(name); gridbag.setConstraints(button, c); add(button); } public void init() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setFont(new Font("Helvetica", Font.PLAIN, 14)); setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; makebutton("Button1", gridbag, c); makebutton("Button2", gridbag, c); makebutton("Button3", gridbag, c); // end row c.gridwidth = GridBagConstraints.REMAINDER; makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default makebutton("Button5", gridbag, c); //another row // next-to last in row c.gridwidth = GridBagConstraints.RELATIVE; makebutton("Button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button7", gridbag, c); c.gridwidth = 1; // reset to the default c.gridheight = 2; c.weighty = 1.0; makebutton("Button8", gridbag, c); c.weighty = 0.0; //reset to the default // end row c.gridwidth = GridBagConstraints.REMAINDER; c.gridheight = 1; // reset to the default makebutton("Button9", gridbag, c); makebutton("Button10", gridbag, c); resize(300, 100); } public static void main(String args[]) { Frame f = new Frame("GridBag Layout Example"); GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.add("Center", ex1); f.pack(); f.show(); } }
protected final static int MAXGRIDSIZE = 128The maximum number of grid positions (both horizontally and vertically) that can be laid out by the grid bag layout.
protected final static int MINSIZE = 1The smallest grid that can be laid out by the grid bag layout.
public GridBagLayout()Creates a grid bag layout manager
public void addLayoutComponent(String name, Component comp)This method is not used by the grid bag layout manager.
Parameter Description name a tag understood by the layout manager comp the component to be added
public GridBagConstraints getConstraints(Component comp)Return Value:
Returns the constraint for the specified component in this grid bag layout. A copy of the constraint object is returned.
Parameter Description comp the component to be queried
public void layoutContainer(Container target)Lays out the container argument using this grid bag layout.
This method reshapes the components in the specified container in order to satisfy the constraints of this GridBagLayout object.
Most applications do not call this method directly. This method is called when a container calls its layout method .
Parameter Description target the container in which to do the layout. See Also: Container .
protected GridBagConstraints lookupConstraints(Component comp)Retrieves the constraints for the specified component in this grid bag layout. The return value is not a copy, but is the actual GridBagConstraints object used by the layout mechanism.
Return Value:
Returns the contraints for the specified component.
Parameter Description comp the component to be queried
public Dimension minimumLayoutSize(Container target)Determines the minimum size of the target container using this grid bag layout.
This method is called when a container calls its layout method . Most applications do not call this method directly.
Return Value:
Returns the minimum dimensions needed to lay out the subcomponents of the specified container.
Parameter Description target the container in which to do the layout See Also: preferredLayoutSize .
public Dimension preferredLayoutSize(Container target)Determines the preferred size of the target container using this grid bag layout.
Most applications do not call this method directly. This method is called when a container calls its preferredSize method .
Return Value:
Returns the preferred dimensions to lay out the subcomponents of the specified container.
Parameter Description target the container in which to do the layout See Also: minimumLayoutSize .
public void removeLayoutComponent(Component comp)Removes the specified component from this layout.
Most applications do not call this method directly. This method is called when a container calls its remove or removeAll methods.
Parameter Description comp the component to be removed
public void setConstraints(Component comp,GridBagConstraints constraints)Sets the constraints for the specified component in this layout
Parameter Description comp the component to be modified constraints the constraints to be applied
public String toString()Return Value:
Returns a string representation of this grid bag layout.
Overrides:
toString in class Object .