Source of CardLayoutDemo.java


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