Chris Johnson, Index
CardLayout - Simpler Application Example
/*
Acknowledgement: based on an application in C.S. Horstmann and G. Cornell's
Core Java 1.1 - volume 1 The Fundamentals.   Sunsoft Press/Prentice Hall, 
Mountain View California, 1998.
*/

import java.awt.*;
import java.awt.event.*;
 
public class CardLayoutTest extends CloseableFrame implements ActionListener
{
	public CardLayoutTest()
	{
		tabs = new Panel();
		addButton("<<");
		addButton("<");
		addButton("Options");
		addButton("Settings");
		addButton("Preferences");
		addButton(">>");
		addButton(">");
		add(tabs, "North");
 
		cards = new Panel();
		layout = new CardLayout();
		cards.setLayout(layout);
 
		cards.add(new SimpleDialog("Option"), "Options");
		cards.add(new SimpleDialog("Setting"), "Setting");
		cards.add(new SimpleDialog("Preferences"), "Preferences");
 
		add(cards, "Center");
	}
 
	public void addButton(String name)
 	{
		Button b = new Button(name);
		b.addActionListener(this);
		tabs.add(b);
	}
 
	public void actionPerformed(ActionEvent evt)
 	{
   		String arg = evt.getActionCommand();
   		if (arg.equals("<<")) layout.first(cards);
   		else if (arg.equals("<")) layout.previous(cards);
   		else if (arg.equals(">")) layout.next(cards);
   		else if (arg.equals(">>")) layout.last(cards);
   		else  layout.show(cards, (String)arg);
 	}
 
	public static void main(String [] args)
 	{
  		Frame f = new CardLayoutTest();
  		f.setSize(400,200);
  		f.show();
 	}
 
 	private Panel cards;
 	private Panel tabs;
 	private CardLayout layout;
}
 
class SimpleDialog extends Panel
{
	SimpleDialog(String name)
  	{
   		add (new Label(name + " dialogue does here"));
  	}
}


forward