Java AWT | CardLayout Class
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:
- CardLayout(): It is used to create a new card layout with gaps of size is zero.
- 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 Classimportjava.awt.*;importjava.awt.event.*;importjavax.swing.JFrame;importjavax.swing.*;// class extends JFrame and implements actionlistenerpublicclassCardlayoutextendsJFrameimplementsActionListener {// Declaration of objects of CardLayout class.CardLayout card;// Declaration of objects of JButton class.JButton b1, b2, b3;// Declaration of objects// of Container class.Container c;Cardlayout(){// to get the contentc = getContentPane();// Initialization of object "card"// of CardLayout class with 40// horizontal space and 30 vertical space .card =newCardLayout(40,30);// set the layoutc.setLayout(card);// Initialization of object "b1" of JButton class.b1 =newJButton("GEEKS");// Initialization of object "b2" of JButton class.b2 =newJButton("FOR");// Initialization of object "b3" of JButton class.b3 =newJButton("GEEKS");// this Keyword refers to current object.// Adding Jbutton "b1" on JFrame using ActionListener.b1.addActionListener(this);// Adding Jbutton "b2" on JFrame using ActionListener.b2.addActionListener(this);// Adding Jbutton "b3" on JFrame using ActionListener.b3.addActionListener(this);// Adding the JButton "b1"c.add("a", b1);// Adding the JButton "b2"c.add("b", b2);// Adding the JButton "b1"c.add("c", b3);}publicvoidactionPerformed(ActionEvent e){// call the next cardcard.next(c);}// Main Methodpublicstaticvoidmain(String[] args){// Creating Object of CardLayout class.Cardlayout cl =newCardlayout();// Function to set size of JFrame.cl.setSize(400,400);// Function to set visibility of JFrame.cl.setVisible(true);// Function to set default operation of JFrame.cl.setDefaultCloseOperation(EXIT_ON_CLOSE);}}chevron_rightfilter_noneOutput:
- 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.importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.*;// class extends JFramepublicclassCardLayoutDemoextendsJFrame {// Initialization the value of// current card is 1 .privateintcurrentCard =1;// Declaration of objects// of CardLayout class.privateCardLayout cl;publicCardLayoutDemo(){// Function to set visibility of JFramesetTitle("Card Layout Example");// Function to set visibility of JFramesetSize(300,150);// Creating Object of "Jpanel" classJPanel cardPanel =newJPanel();// Initialization of object "c1"// of CardLayout class.cl =newCardLayout();// set the layoutcardPanel.setLayout(cl);// Initialization of object// "jp1" of JPanel class.JPanel jp1 =newJPanel();// Initialization of object// "jp2" of CardLayout class.JPanel jp2 =newJPanel();// Initialization of object// "jp3" of CardLayout class.JPanel jp3 =newJPanel();// Initialization of object// "jp4" of CardLayout class.JPanel jp4 =newJPanel();// Initialization of object// "jl1" of JLabel class.JLabel jl1 =newJLabel("Card1");// Initialization of object// "jl2" of JLabel class.JLabel jl2 =newJLabel("Card2");// Initialization of object// "jl3" of JLabel class.JLabel jl3 =newJLabel("Card3");// Initialization of object// "jl4" of JLabel class.JLabel jl4 =newJLabel("Card4");// Adding JPanel "jp1" on JFrame.jp1.add(jl1);// Adding JPanel "jp2" on JFrame.jp2.add(jl2);// Adding JPanel "jp3" on JFrame.jp3.add(jl3);// Adding JPanel "jp4" on JFrame.jp4.add(jl4);// Adding the cardPanel on "jp1"cardPanel.add(jp1,"1");// Adding the cardPanel on "jp2"cardPanel.add(jp2,"2");// Adding the cardPanel on "jp3"cardPanel.add(jp3,"3");// Adding the cardPanel on "jp4"cardPanel.add(jp4,"4");// Creating Object of "JPanel" classJPanel buttonPanel =newJPanel();// Initialization of object// "firstbtn" of JButton class.JButton firstBtn =newJButton("First");// Initialization of object// "nextbtn" of JButton class.JButton nextBtn =newJButton("Next");// Initialization of object// "previousbtn" of JButton class.JButton previousBtn =newJButton("Previous");// Initialization of object// "lastbtn" of JButton class.JButton lastBtn =newJButton("Last");// Adding JButton "firstbtn" on JFrame.buttonPanel.add(firstBtn);// Adding JButton "nextbtn" on JFrame.buttonPanel.add(nextBtn);// Adding JButton "previousbtn" on JFrame.buttonPanel.add(previousBtn);// Adding JButton "lastbtn" on JFrame.buttonPanel.add(lastBtn);// add firstbtn in ActionListenerfirstBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent arg0){// used first c1 CardLayoutcl.first(cardPanel);// value of currentcard is 1currentCard =1;}});// add lastbtn in ActionListenerlastBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent arg0){// used last c1 CardLayoutcl.last(cardPanel);// value of currentcard is 4currentCard =4;}});// add nextbtn in ActionListenernextBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent arg0){// if condition applyif(currentCard <4){// increment the value of currentcard by 1currentCard +=1;// show the value of currentcardcl.show(cardPanel,""+ (currentCard));}}});// add previousbtn in ActionListenerpreviousBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent arg0){// if condition applyif(currentCard >1) {// decrement the value// of currentcard by 1currentCard -=1;// show the value of currentcardcl.show(cardPanel,""+ (currentCard));}}});// used to get content panegetContentPane().add(cardPanel, BorderLayout.NORTH);// used to get content panegetContentPane().add(buttonPanel, BorderLayout.SOUTH);}// Main Methodpublicstaticvoidmain(String[] args){// Creating Object of CardLayoutDemo class.CardLayoutDemo cl =newCardLayoutDemo();// Function to set default operation of JFrame.cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Function to set vivibility of JFrame.cl.setVisible(true);}}chevron_rightfilter_noneOutput:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html
Recommended Posts:
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



