CENTER | NORTHEAST | SOUTHEAST |
EAST | NORTHWEST | SOUTHWEST |
NORTH | SOUTH | WEST |
class GridBagExample extends Frame { public GridBagExample( int width, int height ) { setTitle( "Grid Bag" ); resize( width, height ); GridBagLayout aLayout = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout( aLayout ); constraints.gridx = 0; // Start column constraints.gridy = 0; // Start row constraints.gridwidth = 2; // Num. of columns wide constraints.gridheight = 1; // Num. of rows high constraints.weighty = 100; constraints.weightx = 100; constraints.anchor = GridBagConstraints.CENTER; constraints.fill = GridBagConstraints.NONE; Button none = new Button( "none" ); aLayout.setConstraints( none, constraints ); constraints.gridy = 1; constraints.fill = GridBagConstraints.HORIZONTAL; Button horz = new Button( "horz" ); aLayout.setConstraints( horz, constraints ); constraints.gridy = 2; constraints.fill = GridBagConstraints.BOTH; Button both = new Button( "both" ); aLayout.setConstraints( both, constraints ); constraints.gridy = 3; constraints.fill = GridBagConstraints.VERTICAL; Button vert = new Button( "vert" ); aLayout.setConstraints( vert, constraints ); add( none ); add( both ); add( horz ); add( vert ); show(); } }
class GridBagExample extends Frame { public GridBagExample( int width, int height ) { setTitle( "Grid Bag" ); resize( width, height ); GridBagLayout aLayout = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout( aLayout ); constraints.gridx = 0; // Start column constraints.gridy = 0; // Start row constraints.gridwidth = 1; // Num. of columns wide constraints.gridheight = 1; // Num. rows high constraints.weighty = 100; constraints.weightx = 100; constraints.anchor = GridBagConstraints.CENTER; constraints.fill = GridBagConstraints.NONE; Button center = new Button( "center" ); aLayout.setConstraints( center, constraints ); constraints.gridx = 1; constraints.anchor = GridBagConstraints.WEST; Button west = new Button( "west" ); aLayout.setConstraints( west, constraints ); constraints.gridx = 2; constraints.anchor = GridBagConstraints.NORTHWEST; Button nwest = new Button( "nwest" ); aLayout.setConstraints( nwest, constraints ); constraints.gridx = 3; constraints.anchor = GridBagConstraints.NORTHEAST; Button neast = new Button( "neast" ); aLayout.setConstraints( neast, constraints ); add( center ); add( nwest ); add( west ); add( neast ); show(); } }
class GridBagExample extends Frame { public GridBagExample( int width, int height ) { setTitle( "Insets" ); resize( width, height ); GridBagLayout aLayout = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout( aLayout ); constraints.gridwidth = 1; // Num. of columns wide constraints.gridheight = 1; // Num. rows high int top = 10; int bottom = 20; int left = 12; int right = 0; constraints.insets = new Insets( top, left , bottom, right ); constraints.fill = GridBagConstraints.BOTH; constraints.weighty = 100; constraints.weightx = 100; constraints.gridx = 2; constraints.gridy = 0; Button one = new Button( "one" ); aLayout.setConstraints( one, constraints ); constraints.gridx = 1; Button two = new Button( "two" ); aLayout.setConstraints( two, constraints ); constraints.gridx = 0; Button three = new Button( "three" ); aLayout.setConstraints( three, constraints ); add( one ); add( two ); add( three ); show(); } }
int top = 10; int bottom = 20; int left = 12; int right = 0; constraints.insets = new Insets( top, left , bottom, right );