CardLayout
with the CardLayout()
constructor.
this.setLayout(new CardLayout());
As usual you can create a CardLayout with a specified gap between components like this.
this.setLayout(new CardLayout(3, 4));
In this example there will be a three pixel horizontal gap and a four pixel vertical gap between components.
Each card has a name. A new card is created when you add a component to the card. Add components to cards by passing the name of the card and the component to add to the add()
method like this
this.setLayout(new CardLayout());
this.add("Pizza", new Label("How do you like your pizza?"));
this.add("Pizza", new Button("OK"));
this.add("Payment", new Label("How would you like to pay?"));
this.add("Payment", new Button("OK"));
this.add("Address", new Label("Delivery Instructions"));
this.add("Address", new Button("Order"));
One common technique is to name each card after a number.
this.setLayout(new CardLayout());
this.add("1", new Label("First Card"));
this.add("2", new Label("Second Card"));
this.add("3", new Label("Third Card"));
this.add("4", new Label("Fourth Card"));
this.add("5", new Label("Fifth Card"));
this.add("6", new Label("Sixth Card"));
Internally the list of cards and their names is stored in a java.util.Hashtable
. This means that there's no guaranteed or built-in order to the cards. You need to keep track of this yourself.