Packages This Package Prev Next Index
public class java.awt.GridBagLayout extends java.lang.Object (I-§1.12) implements java.awt.LayoutManager (II-§1.43) { // Fields protected final static int MAXGRIDSIZE; §1.22.1 protected final static int MINSIZE; §1.22.2 // Constructors public GridBagLayout(); §1.22.3 // Methods public void addLayoutComponent(String name, §1.22.4 Component comp); public GridBagConstraints getConstraints(Component comp); §1.22.5 public void layoutContainer(Container target); §1.22.6 protected GridBagConstraints §1.22.7 lookupConstraints(Component comp); public Dimension minimumLayoutSize(Container target); §1.22.8 public Dimension preferredLayoutSize(Container target); §1.22.9 public void removeLayoutComponent(Component comp); §1.22.10 public void §1.22.11 setConstraints(Component comp, GridBagConstraints constraints); public String toString(); §1.22.12 }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.
The fields of the GridBagConstraints object are described more fully in §1.21.
The following figure shows ten components (all buttons) managed by a grid bag layout manager:
In addition, the components have the following non-default constraints:
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 = 128
protected final static int MINSIZE = 1
public GridBagLayout()
public void addLayoutComponent(String name, Component comp)
name
-
a tag understood by the layout manager
comp
-
the component to be added
public GridBagConstraints getConstraints(Component comp)
comp
-
the component to be queried
public void layoutContainer(Container target)
target
-
the container in which to do the layout.
protected GridBagConstraints
lookupConstraints(Component comp)
comp
-
the component to be queried
public Dimension minimumLayoutSize(Container target)
target
-
the container in which to do the layout
public Dimension preferredLayoutSize(Container target)
target
-
the container in which to do the layout
public void removeLayoutComponent(Component comp)
comp
-
the component to be removed
public void setConstraints(Component comp,
GridBagConstraints constraints)
comp
-
the component to be modified
constraints
-
the constraints to be applied
public String toString()
Packages This Package Prev Next IndexJava API Document (HTML generated by dkramer on April 22, 1996)