Algorithm Steps:
Step 1: Write a html applet tag with code set class name and comment the tag
Step 2: Import all necessary packages and classes
Step 3: Define a class that extends applet and implements action listener
Step 4: Declare buttons tiny, large, medium, and small
Step 5: Create panels for card layout and for cards of buttons
Step 6: In the init() method, do the following:
i) Create the card layout
ii) Create the cardpanel and set its layout to card layout
iii) Create other panels and set their layout to border layout
iv) Create buttons and change their fonts to vriuos font face and sizes according to the button name
v) Add action listener to the buttons and each button to one panel of appropriate name
vi) Add the panels to the card panel
vii) Set the layout of the applet to border layout and add card panel to the applet
Step 7: In the actionPerformed() method, move to the next card in the card panel
Java Program To Demonstrate Card Layout
import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.ActionListener;
public class cardLayout extends Applet implements ActionListener
{
private Button tiny,large,medium,small;
private Panel cardpanel=new Panel();
private Panel tinypanel=new Panel();
private Panel smallpanel=new Panel();
private Panel medpanel=new Panel();
private Panel largepanel=new Panel();
private CardLayout card=new CardLayout(10,5);
public void init()
{
cardpanel.setLayout(card);
tinypanel.setLayout(new BorderLayout());
smallpanel.setLayout(new BorderLayout());
medpanel.setLayout(new BorderLayout());
largepanel.setLayout(new BorderLayout());
tiny=new Button("Tiny CardLayout");
small=new Button("Small CardLayout");
medium=new Button("Medium CardLayout");
large=new Button("Large CardLayout");
tiny.setFont(new Font("Helvetica",Font.BOLD,10));
tiny.setForeground(Color.blue);
tiny.setBackground(Color.black);
small.setFont(new Font("Times-Roman",Font.BOLD,23));
small.setForeground(Color.yellow);
small.setBackground(Color.black);
medium.setFont(new Font("Arial",Font.BOLD,32));
medium.setForeground(Color.red);
medium.setBackground(Color.black);
large.setFont(new Font("Courier",Font.BOLD,52));
large.setForeground(Color.green);
large.setBackground(Color.black);
tiny.addActionListener(this);
small.addActionListener(this);
medium.addActionListener(this);
large.addActionListener(this);
tinypanel.add(tiny,BorderLayout.CENTER);
smallpanel.add(small,BorderLayout.CENTER);
medpanel.add(medium,BorderLayout.CENTER);
largepanel.add(large,BorderLayout.CENTER);
cardpanel.add("tiny",tinypanel);
cardpanel.add("small",smallpanel);
cardpanel.add("medium",medpanel);
cardpanel.add("large",largepanel);
setLayout(new BorderLayout());
add(cardpanel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
card.next(cardpanel);
}
}
SAMPLE OUTPUT SCREEN:
|