0% found this document useful (0 votes)
42 views6 pages

AJP Practical

The CardLayout class manages components in a container so that only one component is visible at a time. It treats each component as a card that can be visible or not. Constructors allow setting gaps between cards. Commonly used methods include getting/setting gaps and adding/removing cards from the layout.
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)
42 views6 pages

AJP Practical

The CardLayout class manages components in a container so that only one component is visible at a time. It treats each component as a card that can be visible or not. Constructors allow setting gaps between cards. Commonly used methods include getting/setting gaps and adding/removing cards from the layout.
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/ 6

The CardLayout class manages the components in such a way that only one component is visible

at a time. It treats each component as a card in the container. Only one card is visible at a time,
and the container acts as a stack of cards. The first component added to a CardLayout object is
the visible component when the container is first displayed.
Constructors:
1. CardLayout(): It is used to create a new card layout with gaps of size is zero.
2. CardLayout(int horizontalgap, int verticalgap): It is used to create a new CardLayout class
with the specified horizontal and vertical gaps.
Commonly Used Methods:
 getLayoutAlignmentX(Container parent): Returns the alignment along the x-axis.
 getLayoutAlignmentY(Container parent): Returns the alignment along the y-axis.
 getVgap(): Used to get the vertical gap between components.
 addLayoutComponent(Component cm, Object cn): Used to add the specified component to
this card layout’s internal table of names.
 getHgap(): Used to get the horizontal gap between components.
 toString(): Returns a string representation of the state of this card layout.
 removeLayoutComponent(Component cm): Used to remove the specified component from
the layout.
Below programs illustrate the CardLayout class:
 Program 1: In the below program we are arranging several JLabel components in a JFrame,
whose instance class is “Cardlayout“. We create 3 JButton components named “bt1“, “bt2“,
“bt3” and then add them to the JFrame by the using add() method. We set the size and visibility
of the frame by method setSize() and setVisible(). The layout is set by the
method setLayout() method.

// Java program to illustrate the CardLayout Class

import java.awt.*;

import java.awt.event.*;

import javax.swing.JFrame;

import javax.swing.*;

// class extends JFrame and implements actionlistener

public class Cardlayout extends JFrame implements ActionListener

CardLayout card; // Declaration of objects of CardLayout class.

JButton b1, b2, b3; // Declaration of objects of JButton class.

Container c; // Declaration of objects of Container class.

Cardlayout()
{

c = getContentPane(); // to get the content

card = new CardLayout(40, 30); // Initialization of object "card" of CardLayout class with 40 horizontal
space and 30 vertical space .

c.setLayout(card); // set the layout

b1 = new JButton("GEEKS"); // Initialization of object "b1" of JButton class.

b2 = new JButton("FOR"); // Initialization of object "b2" of JButton class.

b3 = new JButton("GEEKS"); // Initialization of object "b3" of JButton class.

b1.addActionListener(this); // this Keyword refers to current object. Adding Jbutton "b1" on JFrame using
ActionListener.

b2.addActionListener(this); // Adding Jbutton "b2" on JFrame using ActionListener.

b3.addActionListener(this); // Adding Jbutton "b3" on JFrame using ActionListener.

c.add("a", b1); // Adding the JButton "b1"

c.add("b", b2); // Adding the JButton "b2"

c.add("c", b3); // Adding the JButton "b1"

public void actionPerformed(ActionEvent e)

card.next(c); // call the next card

public static void main(String[] args) // Main Method

Cardlayout cl = new Cardlayout(); // Creating Object of CardLayout class.

cl.setSize(400, 400); // Function to set size of JFrame.


cl.setVisible(true); // Function to set visibility of JFrame.

cl.setDefaultCloseOperation(EXIT_ON_CLOSE); // Function to set default operation of JFrame.

 Program 2: In below program we are arranging 4 JLabel components in a JFrame, whose class
“CardlayoutDemo“. We create 4 JButton components named “firstbtn“, “nextbtn“, “previousbtn“, “lastbtn”
and 4 JLabel components named “jl1“, “jl2“, “jl3” “jl4“. Here we are also creating 4 JPanel components
named as “jp1“, “jp2“, “jp3“, “jp4” and then add them to the JFrame by the using add() method. We will set
the size, visibility and title of the frame by using setSize(), setVisible() and setTitle() method respectively. The
layout is set by using setLayout() method.

# Java program to show Example of CardLayout.

// in java. Importing different Package.

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class CardLayoutDemo extends JFrame // class extends JFrame

private int currentCard = 1; // Initialization the value of current card is 1 .

private CardLayout cl; // Declaration of objects of CardLayout class

public CardLayoutDemo()

setTitle("Card Layout Example"); // Function to set visibility of JFrame

setSize(300, 150); // Function to set visibility of JFrame

JPanel cardPanel = new JPanel(); // Creating Object of "Jpanel" class


cl = new CardLayout(); // Initialization of object "c1" of CardLayout class.

cardPanel.setLayout(cl); // set the layout

JPanel jp1 = new JPanel(); // Initialization of object "jp1" of JPanel class.

JPanel jp2 = new JPanel(); // Initialization of object "jp2" of CardLayout class.

JPanel jp3 = new JPanel(); // Initialization of object "jp3" of CardLayout class.

JPanel jp4 = new JPanel(); // Initialization of object "jp4" of CardLayout class.

JLabel jl1 = new JLabel("Card1"); // Initialization of object "jl1" of JLabel class.

JLabel jl2 = new JLabel("Card2"); // Initialization of object "jl2" of JLabel class.

JLabel jl3 = new JLabel("Card3"); // Initialization of object "jl3" of JLabel class.

JLabel jl4 = new JLabel("Card4"); // Initialization of object "jl4" of JLabel class.

jp1.add(jl1); // Adding JPanel "jp1" on JFrame.

jp2.add(jl2); // Adding JPanel "jp2" on JFrame.

jp3.add(jl3); // Adding JPanel "jp3" on JFrame.

jp4.add(jl4); // Adding JPanel "jp4" on JFrame.

cardPanel.add(jp1, "1"); // Adding the cardPanel on "jp1"

cardPanel.add(jp2, "2"); // Adding the cardPanel on "jp2"

cardPanel.add(jp3, "3"); // Adding the cardPanel on "jp3"

cardPanel.add(jp4, "4"); // Adding the cardPanel on "jp4"

JPanel buttonPanel = new JPanel(); // Creating Object of "JPanel" class

JButton firstBtn = new JButton("First"); // Initialization of object "firstbtn" of JButton class.

JButton nextBtn = new JButton("Next"); // Initialization of object "nextbtn" of JButton class.

JButton previousBtn = new JButton("Previous"); // Initialization of object"previousbtn" of JButton class.

JButton lastBtn = new JButton("Last"); // Initialization of object"lastbtn" of JButton class.


buttonPanel.add(firstBtn); // Adding JButton "firstbtn" on JFrame.

buttonPanel.add(nextBtn); // Adding JButton "nextbtn" on JFrame.

buttonPanel.add(previousBtn); // Adding JButton "previousbtn" on JFrame.

buttonPanel.add(lastBtn); // Adding JButton "lastbtn" on JFrame.

firstBtn.addActionListener(new ActionListener() // add firstbtn in ActionListener

public void actionPerformed(ActionEvent arg0)

cl.first(cardPanel); // used first c1 CardLayout

currentCard = 1; // value of currentcard is 1

}});

lastBtn.addActionListener(new ActionListener() // add lastbtn in ActionListener

public void actionPerformed(ActionEvent arg0)

cl.last(cardPanel); // used last c1 CardLayout

currentCard = 4; // value of currentcard is 4

} });

nextBtn.addActionListener(new ActionListener() // add nextbtn in ActionListener

public void actionPerformed(ActionEvent arg0)

if (currentCard < 4) // if condition apply


{

currentCard += 1; // increment the value of currentcard by 1

cl.show(cardPanel, "" + (currentCard)); // show the value of currentcard

} } });

previousBtn.addActionListener(new ActionListener() // add previousbtn in ActionListener

public void actionPerformed(ActionEvent arg0)

if (currentCard > 1 // if condition apply

currentCard -= 1; // decrement the value of currentcard by 1

cl.show(cardPanel, "" + (currentCard)); // show the value of currentcard

} } });

getContentPane().add(cardPanel, BorderLayout.NORTH); // used to get content pane

getContentPane().add(buttonPanel, BorderLayout.SOUTH); // used to get content pane

public static void main(String[] args) // Main Method

CardLayoutDemo cl = new CardLayoutDemo(); // Creating Object of CardLayoutDemo class.

cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Function to set default operation of JFrame.

cl.setVisible(true); // Function to set visibility of JFrame.

You might also like