/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package swingdemo;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author MYPC
 */
public class CardLayoutDemo extends JFrame{

    public CardLayoutDemo(String title) throws HeadlessException {
        super(title);
        this.setSize(400,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        CreatAndShow();
        
    }

    
    
    
    public void  CreatAndShow() throws HeadlessException {
        
        // Tạo Panel cha
        JPanel jpnBorder = new JPanel();
        jpnBorder.setLayout(new BorderLayout());
        
        
        //Tạo một panel phía bắc
        JPanel jpnNorth = new JPanel();
        JButton jbt1 = new JButton("Show Card1");
        JButton jbt2 = new JButton("Show Card2");
        jpnNorth.add(jbt1);
        jpnNorth.add(jbt2);
        jpnBorder.add(jpnNorth, BorderLayout.NORTH);
        
        
        //Tạo panel ở trung tâm
        final JPanel jpnCenter = new JPanel();
        jpnCenter.setLayout(new CardLayout());
        jpnCenter.setBackground(Color.LIGHT_GRAY);
         
        //Tạo một panel bên trong panel trung tâm
        final  JPanel card1 = new JPanel();
        card1.setBackground(Color.LIGHT_GRAY);
        card1.add(new JButton("Hello"));
        card1.add(new JButton("I'm Card1"));
        
        
        //Tạo panel bên trong panel trung tâm 
        //panel này bị panel Card1 chồng lên
        final  JPanel card2 = new JPanel();
        card2.setBackground(Color.PINK);
        card2.add(new JButton("Hi !"));
        card2.add(new JCheckBox("CardLayout"));
        card2.add(new JButton("I'm Card2"));
        
        //Thêm 2 panel này vào panel trung tâm
        jpnCenter.add(card1,"my card1");
        jpnCenter.add(card2,"my card2");
        
        jpnBorder.add(jpnCenter,BorderLayout.CENTER); 
        
        
        Container con = getContentPane();
        con.add(jpnBorder);
        
        
        jbt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              CardLayout c1 = (CardLayout) jpnCenter.getLayout();
            
            c1.show(jpnCenter,"my card1");
                
                
            }
        });
        
        
        jbt2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            CardLayout c2 = (CardLayout) jpnCenter.getLayout();
             c2.show(jpnCenter,"my card2");
                
                
            }
        });
        
      }
    
    public static void main(String[] args) {
        new CardLayoutDemo("MY WINDOW");
    }
    
    
}
