0% found this document useful (0 votes)
43 views2 pages

Practical 4.1 //write A Program To Display Output Using Gridbaglayout

This program uses a GridBagLayout to display buttons in a grid. It creates a GridBagLayout, sets the layout for the JFrame, and adds 5 buttons to the frame at different grid positions using GridBagConstraints. The buttons are arranged horizontally and vertically in a 2x3 grid with the last button spanning 2 columns.

Uploaded by

rohitchavan2345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Practical 4.1 //write A Program To Display Output Using Gridbaglayout

This program uses a GridBagLayout to display buttons in a grid. It creates a GridBagLayout, sets the layout for the JFrame, and adds 5 buttons to the frame at different grid positions using GridBagConstraints. The buttons are arranged horizontally and vertically in a 2x3 grid with the last button spanning 2 columns.

Uploaded by

rohitchavan2345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical 4.

1
//Write a program to display output using GridbagLayout
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class GridBagLayoutExample extends JFrame
{
public static void main(String[] args)
{
GridBagLayoutExample a = new GridBagLayoutExample();
}
public GridBagLayoutExample()
{
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new Button("Button One"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(new Button("Button two"), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
//gbc.ipadx = 0;
gbc.ipady = 80;
gbc.gridx = 0;
gbc.gridy = 1;

this.add(new Button("Button Three"),


gbc);
gbc.gridx = 1;
gbc.gridy = 1;
this.add(new Button("Button
Four"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill =
GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
this.add(new Button("Button Five"),
gbc);
setSize(300, 300);
// setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

You might also like