GridLayout

Grid Layout

The grid layout manager organizes the display into a rectangular grid. It places the components into left to right and top to bottom. The grid layout class has the following constructors:

GridLayout (int numRows, int numCols )

This constructs a grid layout with the specified number of rows and columns. All the components are the equal same size.

Program

1

 

 

 

Program Source

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;

class Frame extends JFrame {
   
    Frame()
    {
        setTitle("GridLayout(4,4)");
        setLayout(new GridLayout(4,4));
        setButton();
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void setButton()
    {
        for(int i=0;i<16;i++)
        {
            add(new JButton("JButton "+i));
        }
    }
}

public class Javaapp {
   
    public static void main(String[] args) {
        
       Frame fr = new Frame();
    }
}

Leave a Comment