Write a program which creates five button of equal size using GridBagLayout in Java Programming

import java.awt.Button; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
 
import javax.swing.*; 
public class Bag extends JFrame{   
        public Bag() { 
   
            GridBagConstraints g = new GridBagConstraints(); 
            GridBagLayout l = new GridBagLayout(); 
    this.setLayout(l); 
    g.fill = GridBagConstraints.HORIZONTAL; 
    g.gridx = 0; 
    g.gridy = 0; 
    this.add(new Button("Button One"), g); 
    g.gridx = 1; 
    g.gridy = 0; 
    this.add(new Button("Button two"), g); 
    g.gridx = 0; 
    g.gridy = 1; 
    this.add(new Button("Button Three"), g); 
    g.gridx = 1; 
    g.gridy = 1; 
    this.add(new Button("Button Four"), g); 
    g.gridx = 0; 
    g.gridy = 2; 
    g.gridwidth = 2; 
    this.add(new Button("Button Five"), g); 
            setSize(300, 300); 
            setVisible(true); 
       
     
        }
public static void main(String[] args) { 
            Bag a = new Bag(); 
        }
     

OP:

Comments