Turn Desktop View Off
by Dinesh Thakur

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

Method

Description

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);
     CardLayoutExample()
     {
         setLayout(card);
         Button Btnfirst = new Button("first ");
         Button BtnSecond = new Button ("Second");
         Button BtnThird = new Button("Third");
         add(Btnfirst,"Card1");
         add(BtnSecond,"Card2");
         add(BtnThird,"Card3");
         Btnfirst.addActionListener(this);
         BtnSecond.addActionListener (this);
         BtnThird.addActionListener(this);
     }
     public void actionPerformed(ActionEvent e)
     {
       card.next(this);
     }
}
   class CardLayoutJavaExample
   {
        public static void main(String args[])
       {
            CardLayoutExample frame = new CardLayoutExample();
            frame.setTitle("CardLayout in Java Example");
            frame.setSize(220,150);
            frame.setResizable(false);
            frame.setVisible(true);
       }
   }

CardLayout in Java Example