TheCardLayout
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.*; public class step4 extends Applet{ // Illustrates Card Layout Frame outerBox = new Frame ( ); Panel cardPanel = new Panel ( ); CardLayout cardManager = new CardLayout ( ); MyPanel buttonPanel = new MyPanel ( this ); Button first = new Button ( "First" ); Button next = new Button ( "Next" ); Button previous = new Button ( "Previous" ); Button last = new Button ( "Last" ); Button button1 = new Button ( "Button 1" ); Button button2 = new Button ( "Button 2" ); Button button3 = new Button ( "Button 3" ); Button button4 = new Button ( "Button 4" ); Button button5 = new Button ( "Button 5" ); Button button6 = new Button ( "Button 6" ); public void init ( ){ 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.setLayout ( cardManager ); cardPanel.add ( button1 ); cardPanel.add ( button2 ); cardPanel.add ( button3 ); cardPanel.add ( button4 ); cardPanel.add ( button5 ); cardPanel.add ( button6 ); buttonPanel.add ( first ); buttonPanel.add ( next ); buttonPanel.add ( previous ); buttonPanel.add ( last ); outerBox.setLayout ( new BorderLayout ( ) ); outerBox.setBackground ( Color.white ); outerBox. setFont (new Font ("Helvetica", Font.PLAIN, 18) ); outerBox.resize (600, 500); outerBox.add ( "North", buttonPanel ); outerBox.add ( "Center", cardPanel ); outerBox.show( ); } // end init public boolean action ( Event event, Object object ) { if ( event.target == first ) { cardManager.first ( cardPanel ); return true; } if ( event.target == next ) { cardManager.next ( cardPanel ); return true; } if ( event.target == previous ) { cardManager.previous ( cardPanel ); return true; } if ( event.target == last ) { cardManager.last ( cardPanel ); return true; } return false; } // end action }// end step4 // ************************ MyPanel Class ************************ class MyPanel extends Panel { Applet source; MyPanel ( Applet a ) { super ( ); source = a; } // end constructor public boolean action ( Event event, Object object ) { if ( source.action ( event, object ) ) return true; else return false; } // end action } // end MyPanelRun the applet