CardLayout

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class CardLayoutDemo
{
    static CardLayout card;
    static JPanel desk;
    public static void main(String ar[])
    {
        
        JPanel p1=new JPanel();p1.setBackground(new Color(234,234,255));
        JPanel p2=new JPanel();p2.setBackground(new Color(200,200,200));
        JPanel p3=new JPanel();p3.setBackground(new Color(200,200,255));
        JPanel p4=new JPanel();p4.setBackground(new Color(100,200,255));
        
        
        JFrame f=new JFrame("New Frame");
        Container c=f.getContentPane();
        c.setLayout(new BorderLayout());
        card=new CardLayout();
        desk=new JPanel();
        desk.setLayout(card);
        desk.add(p1,"p1");
        desk.add(p2,"p2"); 
        desk.add(p3,"p3");
        desk.add(p4,"p4");
        
        
        c.add(desk,BorderLayout.CENTER);
        JPanel buttonpanel=new JPanel();
        JButton next=new JButton("   NEXT >>");
        JButton pre=new JButton("<< BACK    ");
        //Adding Listener using anynomous class
        next.addActionListener(new ActionListener(){
            
            public void actionPerformed(ActionEvent e)
            {
                card.next(desk);
            }
            });
        //Adding Listener using anynomous class
        pre.addActionListener(new ActionListener(){
            
            public void actionPerformed(ActionEvent e)
            {
                card.previous(desk);
            }
            });
        
        buttonpanel.add(pre);
        buttonpanel.add(next);
        c.add(buttonpanel,BorderLayout.SOUTH);
        f.setBounds(100,100,400,300);
        f.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
    
}

OUTPUT

No comments:

Popular Posts