cardlayout in java swings
import java.awt.CardLayout;import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class CardDemo extends JFrame implements ActionListener
{
JButton b1,b2,b3;
CardLayout cl= new CardLayout();
JPanel Page1 = new JPanel();
JPanel Page2 = new JPanel();
JPanel Page3 = new JPanel();
JPanel controlpanel = new JPanel();
CardDemo()
{
setVisible(true);
setSize(550,650);
add(controlpanel);
controlpanel.setLayout(cl);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton n1=new JButton();
//button-1
b1=new JButton("page1");
b1.setBounds(20,30,90,30);
Page1.add(b1);
//Page1.setBackground(Color.red);
Page1.setLayout(null);
//button-2
b2=new JButton("page2");
b2.setBounds(60,30,90,30);
Page2.add(b2);
//Page2.setBackground(Color.darkGray);
Page2.setLayout(null);
//button-3
b3=new JButton("page3");
b3.setBounds(20,90,90,30);
Page3.add(b3);
//Page3.setBackground(Color.GREEN);
Page3.setLayout(null);
Page1.setBackground(Color.red);
Page2.setBackground(Color.yellow);
Page3.setBackground(Color.ORANGE);
controlpanel.add(Page1,"1");
controlpanel.add(Page2,"2");
controlpanel.add(Page3,"3");
cl.show(controlpanel, "1");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
cl.show(controlpanel,"2");
}
if(e.getSource()==b2)
{
cl.show(controlpanel,"3");
}
if(e.getSource()==b3)
{
cl.show(controlpanel,"1");
}
}
public static void main(String[] args)
{
new CardDemo();
}
}
0 Comments