The
CardLayout
manager enables you to create multiple interfaces, like cards in a deck, and shuffle among them.The particular interface to be shown is determined by one of four methods provided by the
CardLayout
manager:first, next, previous,
andlast.
Thus, unlike the other layout managers we have looked at, you must call theCardLayout
manager's methods directly through an instance variable of that type that you create. One way of controlling the interface to be shown is to add a button panel to the display that includes first, next, etc. buttons and select the appropriate interface in anaction
method override.Below is code for a basic CardLayout. Usually, the different "cards" are comprised of containers; in the example, our familiar buttons are used, instead. Since components used in the examples above are reused here in a different way and because a good deal of new code is added, the complete program is shown. Pay particular attention to the variable of type
CardLayout
and its use in theaction
override method.import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class step4 extends Applet implements ActionListener { // Illustrates Card Layout MyFrame outerBox; Panel buttonPanel; Button first; Button next; Button previous; Button last; CardLayout cardManager; Panel cardPanel; Button button1; Button button2; Button button3; Button button4; Button button5; Button button6; public void init ( ){ outerBox = new MyFrame ( ); outerBox.addWindowListener ( outerBox ); buttonPanel = new Panel (); first = new Button ( "First" ); first.addActionListener ( this ); next = new Button ( "Next" ); next.addActionListener ( this ); previous = new Button ( "Previous" ); previous.addActionListener ( this ); last = new Button ( "Last" ); last.addActionListener ( this ); buttonPanel.add ( first ); buttonPanel.add ( next ); buttonPanel.add ( previous ); buttonPanel.add ( last ); cardPanel = new Panel ( ); cardManager = new CardLayout ( ); cardPanel.setLayout ( cardManager ); button1 = new Button ( "Button 1" ); button2 = new Button ( "Button 2" ); button3 = new Button ( "Button 3" ); button4 = new Button ( "Button 4" ); button5 = new Button ( "Button 5" ); button6 = new Button ( "Button 6" ); button1.setBackground ( Color.red ); button2.setBackground ( Color.lightGray ); button3.setBackground ( Color.cyan ); button4.setBackground ( Color.yellow ); button5.setBackground ( Color.green ); button6.setBackground ( Color.orange ); cardPanel.add ( button1, button1.getLabel() ); cardPanel.add ( button2, button2.getLabel() ); cardPanel.add ( button3, button3.getLabel() ); cardPanel.add ( button4, button4.getLabel() ); cardPanel.add ( button5, button5.getLabel() ); cardPanel.add ( button6, button6.getLabel() ); outerBox.setLayout ( new BorderLayout ( ) ); outerBox.setBackground ( Color.white ); outerBox.add ( "North", buttonPanel ); outerBox.add ( "Center", cardPanel ); outerBox.setFont (new Font ("Helvetica", Font.PLAIN, 18) ); outerBox.resize (600, 500); outerBox.show( ); } // end init //*********** Interface Methods *********** //**** ActionListener methods public void actionPerformed ( ActionEvent e ) { Object s = e.getSource(); // *** process Menu actions if ( s instanceof Button ) { // *** help menu items if ( s == first ) { cardManager.first ( cardPanel ); } if ( s == next ) { cardManager.next ( cardPanel ); } if ( s == previous ) { cardManager.previous ( cardPanel ); } if ( s == last ) { cardManager.last ( cardPanel ); } } // end process Button actions } // end actionPerformed }// end step4Run the applet