by Dinesh Thakur Category: AWT and Applets

The java.awt.CardLayout layout manager is significantly different from the other layout managers. Unlike other layout managers, that display all the components within the container at once, a CardLayout layout manager displays only one component at a time (The component could be a component or another container).

Each component in a container with this layout fills the entire container. The name cardlayout evolves from a stack of cards where one card is piled upon another and only one of them is shown. In this layout, the first component that you add to the container will be at the top of stack and therefore visible and the last one will be at the bottom.


Java.awt.CardLayout the class selected the following constructors and methods



CardLayout ()

Creates a new layout manager without spacing between regions.

CardLayout (int, int)

Creates a new layout manager with the spacing horizontal and vertical specified.

first (Container)

Displays the first component added to the layout container specified

getHgap ()

Gets the horizontal spacing.

getVgap ()

Gets the vertical spacing.

last (Container)

Returns the last component added to the layout container specified

next (Container)

Returns the next component added to the layout container specified

previous (Container)

Displays the previous component added to the layout container specified

setHgap (int)

Specifies the horizontal spacing.

setVgap (int)

Specifies the vertical spacing.


import java.awt.*;  

import java.awt.event.*;
class CardLayoutExample extends Frame implements ActionListener
     CardLayout card = new CardLayout(20,20);
         Button Btnfirst = new Button("first ");
         Button BtnSecond = new Button ("Second");
         Button BtnThird = new Button("Third");
         BtnSecond.addActionListener (this);
     public void actionPerformed(ActionEvent e)
   class CardLayoutJavaExample
        public static void main(String args[])
            CardLayoutExample frame = new CardLayoutExample();
            frame.setTitle("CardLayout in Java Example");

CardLayout in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles