Ajp 4
Ajp 4
4
Title: Use of CardLayout to write a program to create a two-level card deck that allows the
user to select an operating system.
Name: Kamal Sharma Enrollment No.: 2115770077
Roll No.: 09 Batch No.: T1
X. Program Code:
Execute the following Program and write the output.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Program:
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
setLayout(grid);
setTitle("GridBag Layout Example");
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.ipady = 20;
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.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
this.add(new Button("Button Five"), gbc);
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output:
Program:
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class GridBagLayoutDemo extends JFrame
{
public static void main(String[] args)
{
GridBagLayoutDemo a = new GridBagLayoutDemo();
}
public GridBagLayoutDemo()
{
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.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new JLabel("Name"), gbc);
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 0;
this.add(new JTextField(20), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
this.add(new JLabel("Comments"), gbc);
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 1;
this.add(new JTextArea(10, 30), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
this.add(new JButton("Submit"), gbc);
setSize(600, 400);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output: