/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.swing.* ; 

2. import java.awt.* ; 

3. import java.awt.event.*; 

4.  

5. public class CLaySw 

6.     extends JApplet implements ActionListener 

7.

8.   JPanel pl1, pl2, crd1, crd2, crd3 ; 

9.   CardLayout clay ; 

10.   JButton b1, b2, b3 ; 

11.   JTextField tf ; 

12.   JTextArea ta ; 

13.   JCheckBox cb ; 

14.   Container cp ; 

15.  

16.   public CLaySw ( ) 

17.   { 

18.     clay = new CardLayout( 10, 5 ) ; 

19.  

20.     pl1 = new JPanel (); 

21.     pl2 = new JPanel (); 

22.  

23.     crd1 = new JPanel (); 

24.     crd2 = new JPanel (); 

25.     crd3 = new JPanel (); 

26.  

27.     b1 = new JButton ( "Show Card Un" ) ; 

28.     b2 = new JButton ( "Show Card Dos" ) ; 

29.     b3 = new JButton ( "Show Card Tres" ) ; 

30.  

31.     tf = new JTextField ( "Whatever" ) ; 

32.     ta = new JTextArea ( "Type\nor don't", 7, 10 ) ; 

33.     cb = new JCheckBox ( "Ambiance", true ) ; 

34.   } 

35.   public void init() 

36.   { 

37.     pl1.setBackground ( Color.orange ) ; 

38.     pl1.add ( b1 ) ; 

39.     pl1.add ( b2 ) ; 

40.     pl1.add ( b3 ) ; 

41.  

42.     pl2.setBackground ( Color.red ) ; 

43.     pl2.setLayout( clay ) ; 

44.     pl2.add ( "un", crd1 ) ; 

45.     pl2.add ( "dos", crd2 ) ; 

46.     pl2.add ( "tres", crd3 ) ; 

47.  

48.     cp = getContentPane(); 

49.     cp.setLayout ( new BorderLayout () ) ; 

50.     cp.add ( pl1,  BorderLayout.NORTH  ) ; 

51.     cp.add ( pl2,  BorderLayout.CENTER ) ; 

52.  

53.     crd1.setBackground ( Color.yellow ) ; 

54.     crd2.setBackground ( Color.green ) ; 

55.     crd3.setBackground ( Color.cyan ) ; 

56.     crd1.add ( new JLabel ("I am the first card, or Panel, rather") ) ; 

57.     crd1.add ( tf ) ; 

58.  

59.     crd2.add ( new JLabel ("The second card, or Panel") ) ; 

60.     crd2.add ( ta ) ; 

61.  

62.     crd3.add ( new JLabel ("El Cardo, el Panel tres") ) ; 

63.     crd3.add ( cb ) ; 

64.  

65.     b1.addActionListener ( this ) ; 

66.     b2.addActionListener ( this ) ; 

67.     b3.addActionListener ( this ) ; 

68.   } 

69.  

70.   public void actionPerformed(ActionEvent ev) 

71.   { 

72.     if ( ev.getSource() == b1 ) 

73.       clay.show ( pl2, "un" ) ; 

74.     else 

75.       if ( ev.getSource() == b2 ) 

76.         clay.show ( pl2, "dos" ) ; 

77.       else 

78.         clay.show ( pl2, "tres" ) ; 

79.   } 

80.