source file name: CardLayoutDemo1.java
import java.awt.*;
import java.awt.event.*;
class circle extends Canvas
{
public circle() {
setBackground (Color.gray);
setSize(300, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(100,100,80,80);
}
}
class square extends Canvas
{
public square() {
setBackground (Color.gray);
setSize(300, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fillRect(100,100,80,80);
}
}
class rect extends Canvas
{
public rect() {
setBackground (Color.gray);
setSize(300, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(100,100,180,80);
}
}
class CardLayoutDemo1 extends Frame implements ActionListener
{
CardLayout card;
Button b1,b2,b3;
Panel pc,pbt;
public CardLayoutDemo1()
{
BorderLayout bl=new BorderLayout();
setLayout(bl);
pc=new Panel();
pbt=new Panel();
card=new CardLayout(40,30);
pc.setLayout(card);
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
p1.add(new circle() );
p2.add(new rect());
p3.add(new square());
b1=new Button("Circle");
b2=new Button("Rectangle");
b3=new Button("Square");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
pc.add("a",p1);pc.add("b",p2);pc.add("c",p3);
pbt.add(b1);
pbt.add(b2);
pbt.add(b3);
add(pbt,BorderLayout.SOUTH);add(pc,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{
card.next(pc);
}
public static void main(String[] args)
{
CardLayoutDemo1 f=new CardLayoutDemo1();
f.setTitle("Card Layout Demo 1");
f.setSize(400,400);
f.setVisible(true);
}
}
output:
click buttons you get three shapes output
0 Comments
Post a Comment