0% found this document useful (0 votes)
254 views

Level Card Deck That Allows The User To Select An Operating System

The document describes a Java program that uses CardLayout to create a two-level card deck that allows the user to select an operating system. The program contains buttons labeled "Apple", "Boy", and "Cat" and uses the CardLayout to switch between these buttons. When each button is clicked, the label of the currently displayed button is printed. The document also contains questions and exercises related to using GridLayout and GridBagLayout in Java programs.

Uploaded by

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

Level Card Deck That Allows The User To Select An Operating System

The document describes a Java program that uses CardLayout to create a two-level card deck that allows the user to select an operating system. The program contains buttons labeled "Apple", "Boy", and "Cat" and uses the CardLayout to switch between these buttons. When each button is clicked, the label of the currently displayed button is printed. The document also contains questions and exercises related to using GridLayout and GridBagLayout in Java programs.

Uploaded by

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

Enrollment No : 1901160126

Name : Siddhi Dhanaji Sawant


Roll No. : 23
Branch : Information Technology
Semester : 5th

Practical No. : 04
------------------------------------------------------------------------------
Aim : Use of CardLayout to write a program to create a two-
level card deck that allows the user to select an operating
system.

X.Program Code
 Execute the following Program and write the output.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample extends JFrame implements
ActionListener
{
CardLayout card;
JButton b1, b2, b3;
Container c;
CardLayoutExample()
{
c=getContentPane();
card=new CardLayout(40,30);
//create CardLayout object with 40 hor space and 30 ver
space
c.setLayout(card);
b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);
c.add("b",b2);
c.add("c",b3);
}
public void actionPerformed(ActionEvent e)
{
card.next(c);
}
public static void main(String[] args)
{
CardLayoutExample cl=new CardLayoutExample();
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Output :

After click on Apple :

After click on Boy :


XII. Practical Related Questions
1. State difference between GridLayout and GridBagLayout.
Answer : GridLayout : A GridLayout arranges the
components in a rectangular grid. It arranges component in
the cells and each cell has the same size. Components are
placed in columns and rows. GridLayout(int rows, int
columns) takes two parameters that are a column and a row.
GridBagLayout : A GridBagLayout extends the capabilities
of the GridLayout. GridBagLayout places component in each
individual cell in a grid and also allows the component to
span to multiple columns or rows. In order to
use GridBagLayout, we need to create.

2. Explain constructor of GridbagLayout.


Answer :  Each GridBagLayout object manages a rectangular
grid of cells, dynamic with each component occupying one
or more cells, called its display area. GridBagLayout
components are associated with the instance of
GridBagConstraints. These constraints are used to define the
component’s display area and their positions.

XIII.Exercise
1. Write Java program to display following output.

Program Code :
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;

public class GridExample extends JFrame{


public static void main(String[] args) {

GridExample ge = new GridExample();


}

public GridExample() {

GridBagLayout gl = new GridBagLayout();

GridBagConstraints gc = new GridBagConstraints();

setLayout(gl);

setTitle("Example for GridBag Layout ");


GridBagLayout ll = new GridBagLayout();
this.setLayout(ll);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 0;
this.add(new Button("Button 1"), gc);
gc.gridx = 1;
gc.gridy = 0;
this.add(new Button("Button 2"), gc);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.ipady = 20;
gc.gridx = 0;
gc.gridy = 1;
this.add(new Button("Button 3"), gc);
gc.gridx = 1;
gc.gridy = 1;
this.add(new Button("Button 4"), gc);
gc.gridx = 0;
gc.gridy = 2;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridwidth = 2;
this.add(new Button("Button 5"), gc);
setSize(200, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

2. Write Java program to display following output.

Program Code :
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;

public class Pra4_b extends JFrame {

public Pra4_b() {
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
JLabel L1 = new JLabel("Name");
JLabel L2 = new JLabel("Comments");
JTextField T1 = new JTextField();
JTextArea T2 = new JTextArea(10, 10);
JScrollPane SP = new JScrollPane(T2);

SP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_S
CROLLBAR_ALWAYS);
this.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(L1, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(T1, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
this.add(L2, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
this.add(T2, gbc);
this.getContentPane().add(SP);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
this.add(new Button("Submit"), gbc);
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Pra4_b a = new Pra4_b();
}
}

You might also like