public class CardLayoutDemo extends JFrame implements ActionListener
1: import javax.swing.JButton;
2: import javax.swing.JFrame;
3: import javax.swing.JLabel;
4: import javax.swing.JPanel;
5: import java.awt.BorderLayout;
6: import java.awt.CardLayout;
7: import java.awt.FlowLayout;
8: import java.awt.Color;
9: import java.awt.Container;
10: import java.awt.event.ActionEvent;
11: import java.awt.event.ActionListener;
12:
13: public class CardLayoutDemo extends JFrame implements ActionListener
14: {
15: public static final int WIDTH = 300;
16: public static final int HEIGHT = 200;
17:
18: private CardLayout dealer;
19: private JPanel deckPanel;
20:
21: public CardLayoutDemo( )
22: {
23: setSize(WIDTH, HEIGHT);
24: addWindowListener(new WindowDestroyer( ));
25: setTitle("CardLayout Demonstration");
26: Container contentPane = getContentPane( );
27: contentPane.setLayout(new BorderLayout( ));
28:
29: deckPanel = new JPanel( );
30: dealer = new CardLayout( );
31: deckPanel.setLayout(dealer);
32:
33: JPanel startCardPanel = new JPanel( );
34: startCardPanel.setLayout(new FlowLayout( ));
35: startCardPanel.setBackground(Color.LIGHT_GRAY);
36: JLabel startLabel = new JLabel("Hello");
37: startCardPanel.add(startLabel);
38: deckPanel.add("start", startCardPanel);
39:
40: JPanel greenCardPanel = new JPanel( );
41: greenCardPanel.setLayout(new FlowLayout( ));
42: greenCardPanel.setBackground(Color.GREEN);
43: JLabel goLabel = new JLabel("Go");
44: greenCardPanel.add(goLabel);
45: deckPanel.add("green", greenCardPanel);
46:
47: JPanel redCardPanel = new JPanel( );
48: redCardPanel.setLayout(new FlowLayout( ));
49: redCardPanel.setBackground(Color.RED);
50: JLabel stopLabel = new JLabel("Stop");
51: redCardPanel.add(stopLabel);
52: deckPanel.add("red", redCardPanel);
53:
54: contentPane.add(deckPanel, BorderLayout.CENTER);
55:
56: JPanel buttonPanel = new JPanel( );
57: buttonPanel.setBackground(Color.WHITE);
58: buttonPanel.setLayout(new FlowLayout( ));
59: JButton stopButton = new JButton("Red");
60: stopButton.addActionListener(this);
61: buttonPanel.add(stopButton);
62: JButton goButton = new JButton("Green");
63: goButton.addActionListener(this);
64: buttonPanel.add(goButton);
65: JButton resetButton = new JButton("Reset");
66: resetButton.addActionListener(this);
67: buttonPanel.add(resetButton);
68: contentPane.add(buttonPanel, BorderLayout.SOUTH);
69: dealer.first(deckPanel);//Optional
70: }
71:
72:
73: public void actionPerformed(ActionEvent e)
74: {
75: String actionCommand = e.getActionCommand( );
76:
77: if (actionCommand.equals("Red"))
78: dealer.show(deckPanel, "red");
79: else if (actionCommand.equals("Green"))
80: dealer.show(deckPanel, "green");
81: else if (actionCommand.equals("Reset"))
82: dealer.show(deckPanel, "start");
83: else
84: System.out.println("Error in CardLayout Demo.");
85: }
86:
87:
88: public static void main(String[] args)
89: {
90: CardLayoutDemo demoGui = new CardLayoutDemo( );
91: demoGui.setVisible(true);
92: }
93: }
94:
95: