1 import java.applet.Applet; 2 import java.awt.*; 3 4 public class CardLayout1 extends Applet { 5 private CardLayout cardLayout; 6 private Panel cardPanel; 7 private List list; 8 9 public void init() { 10 cardLayout = new CardLayout(); 11 Panel controlPanel = new Panel(); 12 controlPanel.setBackground(Color.pink); 13 controlPanel.add(new Button("Buttons")); 14 controlPanel.add(new Button("TextFields")); 15 controlPanel.add(new Button("Lists")); 16 17 cardPanel = new Panel(); 18 19 cardPanel.setLayout(cardLayout); 20 Panel buttonsPanel = new Panel(); 21 buttonsPanel.setBackground(Color.yellow); 22 buttonsPanel.add(new Button("Button 1")); 23 buttonsPanel.add(new Button("Button 2")); 24 buttonsPanel.add(new Button("Button 3")); 25 Panel textFieldsPanel = new Panel(); 26 textFieldsPanel.setBackground(Color.cyan); 27 textFieldsPanel.add(new TextField(10)); 28 String msg = "Please enter your name"; 29 textFieldsPanel.add(new TextField(msg, 40)); 30 Panel listsPanel = new Panel(); 31 listsPanel.setBackground(Color.magenta); 32 list = new List(5, false); 33 list.addItem("Hamlet"); 34 list.addItem("Claudius"); 35 list.addItem("Gertrude"); 36 list.addItem("Polonius"); 37 list.addItem("Horatio"); 38 list.addItem("Laertes"); 39 list.addItem("Ophelia"); 40 list.addItem("Caesar"); 41 list.addItem("Brutus"); 42 list.addItem("Alexandrius"); 43 listsPanel.add(list); 44 Panel welcomePanel = new Panel(); 45 welcomePanel.setBackground(Color.gray); 46 welcomePanel.add(new Label("Welcome to an example of CardLayout")); 47 cardPanel.add("card 1", welcomePanel); 48 cardPanel.add("card 2", buttonsPanel); 49 cardPanel.add("card 3", textFieldsPanel); 50 cardPanel.add("card 4", listsPanel); 51 setLayout(new BorderLayout()); 52 add("North",controlPanel); 53 add("Center",cardPanel); 54 } 55 56 public boolean action(Event evt, Object arg) { 57 if (arg.equals("Buttons")) { 58 cardLayout.show(cardPanel, "card 2"); 59 } 60 else if (arg.equals("TextFields")) { 61 cardLayout.show(cardPanel, "card 3"); 62 } 63 else if (arg.equals("Lists")) { 64 cardLayout.show(cardPanel, "card 4"); 65 } 66 else return super.action(evt,arg); 67 return true; 68 } 69 }