The
GridBagLayout
manager is the most flexible but most complex of the layout mangers. This discussion is intended to get you started, but because so many constraints are provided and because they interact with one another, you should plan to spend time experimenting with different values and combinations. You should also review the specification for the class .The idea behind GridBagLayout is that you divide the display into cells (rows and columns), similar to a GridLayout. However, the difference is that the various cells can have different sizes. Furthermore, as the window is resized, the components within the cells are resized in accord with their respective constraints and not uniformly.
Like the
CardLayout
manager, you will need to create an instance variable of that type.Most of the work in using GridBagLayout is in setting values for the different variables included in the
GridBagConstraint
class.Some of the variables that can be set in
GridBagConstraints
are the following:
anchor
specifies the compass point where the component will be anchoredfill
specifies whether and in what directions the component may expandgridx
identifies the column in which the component beginsgridy
identifies the row in which the component beginsinsets
specifies border values around the componentweightx
determines how much or how little the component may be resized horizontallyweighty
determines how much or how little the component may be resized verticallyOnce the constraints are set for a given component, the
setConstraints
method is used for theGridBagLayout
object to which the component and the constraints object are passed.Below is code for a basic GridBag Layout. Note the
addComponent
class that is used to simplify the setting of constraints.import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class step5 extends Applet{ // Illustrates GridBagLayout MyFrame outerBox; Panel pad; Button button0; Button button1; Button button2; Button button3; Button button4; Button button5; Button button6; GridBagLayout gbLayout; GridBagConstraints gbConstraints; public void init ( ){ outerBox = new MyFrame ( ); outerBox.addWindowListener ( outerBox ); button0 = new Button ( ); button1 = new Button ( "Button 1" ); button2 = new Button ( "Button 2" ); button3 = new Button ( "Button 3" ); button4 = new Button ( "Button 4" ); button5 = new Button ( "Button 5" ); button6 = new Button ( "Button 6" ); button0.setBackground ( new Color ( 239, 192, 255 ) ); button1.setBackground ( Color.red ); button2.setBackground ( Color.lightGray ); button3.setBackground ( Color.cyan ); button4.setBackground ( Color.yellow ); button5.setBackground ( Color.green ); button6.setBackground ( Color.orange ); pad = new Panel ( ); pad.setBackground ( Color.blue); gbLayout = new GridBagLayout ( ); gbConstraints = new GridBagConstraints ( ); outerBox.setLayout ( gbLayout ); outerBox.setBackground ( Color.white ); outerBox. setFont (new Font ("Helvetica", Font.PLAIN, 18) ); outerBox.resize (600, 500); gbConstraints.insets= new Insets ( 0, 0, 20, 20); gbConstraints.anchor= GridBagConstraints.NORTHWEST; gbConstraints.fill = GridBagConstraints.BOTH; gbConstraints.weightx = 1000; gbConstraints.weighty = 1; addComponent ( outerBox, button0, gbLayout, gbConstraints, 0, 0, 3, 3 ); gbConstraints.anchor= GridBagConstraints.NORTHEAST; gbConstraints.fill = GridBagConstraints.NONE; gbConstraints.weightx = 0; gbConstraints.weighty = 0; addComponent ( outerBox, button1, gbLayout, gbConstraints, 3, 0, 1, 1 ); addComponent ( outerBox, button3, gbLayout, gbConstraints, 3, 1, 1, 1 ); addComponent ( outerBox, button5, gbLayout, gbConstraints, 3, 2, 1, 1 ); gbConstraints.anchor= GridBagConstraints.NORTHWEST; gbConstraints.weightx = 1000; gbConstraints.fill = GridBagConstraints.HORIZONTAL; addComponent ( outerBox, button2, gbLayout, gbConstraints, 4, 0, 4, 1 ); addComponent ( outerBox, button4, gbLayout, gbConstraints, 4, 1, 4, 1 ); addComponent ( outerBox, button6, gbLayout, gbConstraints, 4, 2, 4, 1 ); gbConstraints.anchor= GridBagConstraints.NORTHWEST; gbConstraints.fill = GridBagConstraints.BOTH; gbConstraints.weightx = 1000; gbConstraints.weighty = 1000; addComponent ( outerBox, pad, gbLayout, gbConstraints, 0, 3, 8, 1 ); outerBox.show( ); } // end init public void addComponent ( Container cont, Component comp, GridBagLayout gbl, GridBagConstraints gbc, int col, int row, int wid, int hgt ) { gbc.gridx = col; gbc.gridy = row; gbc.gridwidth = wid; gbc.gridheight = hgt; gbl.setConstraints ( comp, gbc ); cont.add ( comp ); } // end addComponent }// end step5Run the applet