1 // File: CardLayout1.java 2 3 import java.applet.Applet; 4 import java.awt.*; 5 import java.awt.event.*; 6 7 public class CardLayout1 extends Applet implements ActionListener 8 { 9 private CardLayout cardLayout; 10 private Panel cardPanel; 11 private Panel controlPanel; 12 private List list; 13 private Button Buttons,TextFields, Lists; 14 15 public void init() 16 { // the north cell of the applet has this controlPanel with 3 buttons 17 controlPanel = new Panel(); 18 controlPanel.setBackground(Color.pink); 19 controlPanel.add(Buttons=new Button("Buttons")); 20 controlPanel.add(TextFields=new Button("TextFields")); 21 controlPanel.add(Lists=new Button("Lists")); 22 Buttons.addActionListener(this); 23 TextFields.addActionListener(this); 24 Lists.addActionListener(this); 25 26 // the center cell of the applet has this cardPanel with 4 panels 27 // to show at different times 28 cardPanel = new Panel(); 29 cardLayout = new CardLayout(); 30 cardPanel.setLayout(cardLayout); 31 32 // this panel is "card 2" in the CardLayout 33 Panel buttonsPanel = new Panel(); 34 // contents are some buttons with no events just to show concept 35 buttonsPanel.setBackground(Color.yellow); 36 buttonsPanel.add(new Button("Button 1")); 37 buttonsPanel.add(new Button("Button 2")); 38 buttonsPanel.add(new Button("Button 3")); 39 40 // this panel is "card 3" in the CardLayout (textfields have no events) 41 Panel textFieldsPanel = new Panel(); 42 textFieldsPanel.setBackground(Color.cyan); 43 textFieldsPanel.add(new TextField(10)); 44 String msg = "Please enter your name"; 45 textFieldsPanel.add(new TextField(msg, 40)); 46 47 // this panel is "card 4" in the CardLayout (list has no events) 48 Panel listsPanel = new Panel(); 49 listsPanel.setBackground(Color.magenta); 50 list = new List(5, false); 51 list.addItem("Hamlet"); 52 list.addItem("Claudius"); 53 list.addItem("Gertrude"); 54 list.addItem("Polonius"); 55 list.addItem("Horatio"); 56 list.addItem("Laertes"); 57 list.addItem("Ophelia"); 58 list.addItem("Caesar"); 59 list.addItem("Brutus"); 60 list.addItem("Alexandrius"); 61 listsPanel.add(list); 62 63 // this panel is "card 1" in the CardLayout 64 Panel welcomePanel = new Panel(); 65 welcomePanel.setBackground(Color.gray); 66 welcomePanel.add(new Label("Welcome to an example of CardLayout")); 67 68 // add the panels to the CardLayout 69 cardPanel.add("card 1", welcomePanel); 70 cardPanel.add("card 2", buttonsPanel); 71 cardPanel.add("card 3", textFieldsPanel); 72 cardPanel.add("card 4", listsPanel); 73 74 // layout of the applet 75 setLayout(new BorderLayout()); 76 add("North",controlPanel); 77 add("Center",cardPanel); 78 } 79 80 public void actionPerformed( ActionEvent e) 81 { 82 // respond to the applet buttons by showing different card panels 83 if (e.getSource()==Buttons) 84 { 85 cardLayout.show(cardPanel, "card 2"); 86 } 87 else if (e.getSource()==TextFields) 88 { 89 cardLayout.show(cardPanel, "card 3"); 90 } 91 else if (e.getSource()==Lists) 92 { 93 cardLayout.show(cardPanel, "card 4"); 94 } 95 } 96 } 97 98 99 100