GridLayout horizontal and vertical gaps

The grid layout class has the following constructors:

GridLayout( )

GridLayout(int numRows, int numColumns)

GridLayout(int numRows, int numColumns, int horz, int vert)

The first form creates a single-column grid layout. The second form creates a grid layout with the specified number of rows and columns. The third form allows you to specify the horizontal and vertical space left between components in horz and vert.

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,10,10)");
        setLayout(new GridLayout(4,4,10,10));
        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