The GridLayout Manager Panels and Complex Layouts

J.E.D.I. Figure 7.5: Running BorderLayoutDemo

7.4.3 The GridLayout Manager

With the GridLayout manager, components are also positioned from left to right and top to bottom as in the FlowLayout manager. In addition to this, the GridLayout manager divides the container into a number of rows and columns. All these regions are equally sized. It always ignores the components preferred size. The following is the available constructors for the GridLayout class. GridLayout Constructors GridLayout Creates a new GridLayout object with a single row and a single column by default. GridLayoutint rows, int cols Creates a new GridLayout object with the specified number of rows and columns. GridLayoutint rows, int cols, int hgap, int vgap Creates a new GridLayout object with the specified number of rows and columns. hgap- unit horizontal and vgap-unit vertical spacings are applied to the components. Table 21: GridLayout constructors Try out this program. import java.awt.; class GridLayoutDemo extends Frame { public static void mainString args[] { GridLayoutDemo gld = new GridLayoutDemo; gld.setLayoutnew GridLayout2, 3, 4, 4; gld.addnew ButtonONE; gld.addnew ButtonTWO; gld.addnew ButtonTHREE; gld.addnew ButtonFOUR; gld.addnew ButtonFIVE; gld.setSize200, 200; gld.setVisibletrue; } } This is the output of the program. Observe the effect of resizing the frame. Introduction to Programming II Page 99 J.E.D.I. Figure 7.6: Running GridLayoutDemo

7.4.4 Panels and Complex Layouts

To create more complex layouts, you can combine the different layout managers with the use of panels. Remember that a Panel is a Container and a Component at the same time. You can insert Components into the Panel and then add the Panel to a specified region in the Container. Observe the technique used in the following example. import java.awt.; class ComplexLayout extends Frame { public static void mainString args[] { ComplexLayout cl = new ComplexLayout; Panel panelNorth = new Panel; Panel panelCenter = new Panel; Panel panelSouth = new Panel; North Panel Panels use FlowLayout by default panelNorth.addnew ButtonONE; panelNorth.addnew ButtonTWO; panelNorth.addnew ButtonTHREE; Center Panel panelCenter.setLayoutnew GridLayout4,4; panelCenter.addnew TextField1st; panelCenter.addnew TextField2nd; panelCenter.addnew TextField3rd; panelCenter.addnew TextField4th; South Panel panelSouth.setLayoutnew BorderLayout; Introduction to Programming II Page 100 J.E.D.I. panelSouth.addnew CheckboxChoose me, BorderLayout.CENTER; panelSouth.addnew CheckboxIm here, BorderLayout.EAST; panelSouth.addnew CheckboxPick me, BorderLayout.WEST; Adding the Panels to the Frame container Frames use BorderLayout by default cl.addpanelNorth, BorderLayout.NORTH; cl.addpanelCenter, BorderLayout.CENTER; cl.addpanelSouth, BorderLayout.SOUTH; cl.setSize300,300; cl.setVisibletrue; } } Here is the output of the program. Introduction to Programming II Page 101 J.E.D.I. Figure 7.7: Running ComplexLayout

7.5 Swing GUI Components