|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.GridBagLayout
GridBagLayout is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be the same size. Each GridBagLayout uses a rectangular grid of cells, with each component occupying one or more cells (called its display area). Each component managed by a GridBagLayout is associated with a GridBagConstraints instance that specifies how the component is laid out within its display area. How a GridBagLayout places a set of components depends on each component's GridBagConstraints and minimum size, as well as the preferred size of the components' container.
To use a GridBagLayout effectively, you must customize one or more of its components' GridBagConstraints. You customize a GridBagConstraints object by setting one or more of its instance variables:
All the components have fill=GridBagConstraints.BOTH. 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); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default makebutton("Button5", gridbag, c); //another row c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row 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 c.gridwidth = GridBagConstraints.REMAINDER; //end row 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.resize(f.preferredSize()); f.show(); } }
Field Summary | |
double[] |
columnWeights
|
int[] |
columnWidths
|
protected Hashtable |
comptable
|
protected GridBagConstraints |
defaultConstraints
|
protected java.awt.GridBagLayoutInfo |
layoutInfo
|
protected static int |
MAXGRIDSIZE
|
protected static int |
MINSIZE
|
protected static int |
PREFERREDSIZE
|
int[] |
rowHeights
|
double[] |
rowWeights
|
Constructor Summary | |
GridBagLayout()
Creates a gridbag layout. |
Method Summary | |
void |
addLayoutComponent(String name,
Component comp)
Adds the specified component with the specified name to the layout. |
protected void |
AdjustForGravity(GridBagConstraints constraints,
Rectangle r)
|
protected void |
ArrangeGrid(Container parent)
|
GridBagConstraints |
getConstraints(Component comp)
Retrieves the constraints for the specified component. |
int[][] |
getLayoutDimensions()
|
protected java.awt.GridBagLayoutInfo |
GetLayoutInfo(Container parent,
int sizeflag)
Print the layout constraints. |
Point |
getLayoutOrigin()
|
double[][] |
getLayoutWeights()
|
protected Dimension |
GetMinSize(Container parent,
java.awt.GridBagLayoutInfo info)
|
void |
layoutContainer(Container parent)
Lays out the container in the specified panel. |
Point |
location(int x,
int y)
|
protected GridBagConstraints |
lookupConstraints(Component comp)
Retrieves the constraints for the specified component. |
Dimension |
minimumLayoutSize(Container parent)
Returns the minimum dimensions needed to layout the components contained in the specified panel. |
Dimension |
preferredLayoutSize(Container parent)
Returns the preferred dimensions for this layout given the components in the specified panel. |
void |
removeLayoutComponent(Component comp)
Removes the specified component from the layout. |
void |
setConstraints(Component comp,
GridBagConstraints constraints)
Sets the constraints for the specified component. |
String |
toString()
Returns the String representation of this GridLayout's values. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected static final int MAXGRIDSIZE
protected static final int MINSIZE
protected static final int PREFERREDSIZE
protected Hashtable comptable
protected GridBagConstraints defaultConstraints
protected java.awt.GridBagLayoutInfo layoutInfo
public int[] columnWidths
public int[] rowHeights
public double[] columnWeights
public double[] rowWeights
Constructor Detail |
public GridBagLayout()
Method Detail |
public void setConstraints(Component comp, GridBagConstraints constraints)
comp
- the component to be modifiedconstraints
- the constraints to be appliedpublic GridBagConstraints getConstraints(Component comp)
comp
- the component to be queriedprotected GridBagConstraints lookupConstraints(Component comp)
comp
- the component to be queriedpublic Point getLayoutOrigin()
public int[][] getLayoutDimensions()
public double[][] getLayoutWeights()
public Point location(int x, int y)
public void addLayoutComponent(String name, Component comp)
addLayoutComponent
in interface LayoutManager
name
- the name of the componentcomp
- the component to be addedpublic void removeLayoutComponent(Component comp)
removeLayoutComponent
in interface LayoutManager
comp
- the component to be removedpublic Dimension preferredLayoutSize(Container parent)
preferredLayoutSize
in interface LayoutManager
parent
- the component which needs to be laid outminimumLayoutSize(java.awt.Container)
public Dimension minimumLayoutSize(Container parent)
minimumLayoutSize
in interface LayoutManager
parent
- the component which needs to be laid outpreferredLayoutSize(java.awt.Container)
public void layoutContainer(Container parent)
layoutContainer
in interface LayoutManager
parent
- the specified component being laid outContainer
public String toString()
toString
in class Object
protected java.awt.GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag)
protected void AdjustForGravity(GridBagConstraints constraints, Rectangle r)
protected Dimension GetMinSize(Container parent, java.awt.GridBagLayoutInfo info)
protected void ArrangeGrid(Container parent)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |