// File: CardLayout1.java import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class CardLayout1 extends Applet implements ActionListener { private CardLayout cardLayout; private Panel cardPanel; private Panel controlPanel; private List list; private Button Buttons,TextFields, Lists; public void init() { // the north cell of the applet has this controlPanel with 3 buttons controlPanel = new Panel(); controlPanel.setBackground(Color.pink); controlPanel.add(Buttons=new Button("Buttons")); controlPanel.add(TextFields=new Button("TextFields")); controlPanel.add(Lists=new Button("Lists")); Buttons.addActionListener(this); TextFields.addActionListener(this); Lists.addActionListener(this); // the center cell of the applet has this cardPanel with 4 panels // to show at different times cardPanel = new Panel(); cardLayout = new CardLayout(); cardPanel.setLayout(cardLayout); // this panel is "card 2" in the CardLayout Panel buttonsPanel = new Panel(); // contents are some buttons with no events just to show concept buttonsPanel.setBackground(Color.yellow); buttonsPanel.add(new Button("Button 1")); buttonsPanel.add(new Button("Button 2")); buttonsPanel.add(new Button("Button 3")); // this panel is "card 3" in the CardLayout (textfields have no events) Panel textFieldsPanel = new Panel(); textFieldsPanel.setBackground(Color.cyan); textFieldsPanel.add(new TextField(10)); String msg = "Please enter your name"; textFieldsPanel.add(new TextField(msg, 40)); // this panel is "card 4" in the CardLayout (list has no events) Panel listsPanel = new Panel(); listsPanel.setBackground(Color.magenta); list = new List(5, false); list.addItem("Hamlet"); list.addItem("Claudius"); list.addItem("Gertrude"); list.addItem("Polonius"); list.addItem("Horatio"); list.addItem("Laertes"); list.addItem("Ophelia"); list.addItem("Caesar"); list.addItem("Brutus"); list.addItem("Alexandrius"); listsPanel.add(list); // this panel is "card 1" in the CardLayout Panel welcomePanel = new Panel(); welcomePanel.setBackground(Color.gray); welcomePanel.add(new Label("Welcome to an example of CardLayout")); // add the panels to the CardLayout cardPanel.add("card 1", welcomePanel); cardPanel.add("card 2", buttonsPanel); cardPanel.add("card 3", textFieldsPanel); cardPanel.add("card 4", listsPanel); // layout of the applet setLayout(new BorderLayout()); add("North",controlPanel); add("Center",cardPanel); } public void actionPerformed( ActionEvent e) { // respond to the applet buttons by showing different card panels if (e.getSource()==Buttons) { cardLayout.show(cardPanel, "card 2"); } else if (e.getSource()==TextFields) { cardLayout.show(cardPanel, "card 3"); } else if (e.getSource()==Lists) { cardLayout.show(cardPanel, "card 4"); } } }