Java AWT GridLayout Manager

setLayout(new GridLayout(3, 4));
setBackground(Color.red);

The frame is divided into 3 rows and 4 columns comprising of 12 cells. In each cell, one button is added. The background color red is not seen as the default gap between the components is 0 pixels. The following statement gives the gap.

setLayout(new GridLayout(3, 4, 10, 20));

The extra last two parameters 10 and 20 indicates horizontal gap and vertical gap. As in the previous statement, 3 and 4 indicate rows and columns. Now the background color red is seen. Horizontal gap means the gap between the components of a row. Vertical gap means the gap between the components of two rows.

The knowledge of FlowLayout and BorderLayout is very essential for layout management. It is required to know about Panels also to get the desired layout of components.

The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code close icon to work.

Leave a Comment

Your email address will not be published.