Card Layout

//<applet code=CardLayoutDemo height=500 width=500></applet>
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo extends JApplet implements ActionListener {
	JPanel mp;
	CPanel1 p1;
	CPanel2 p2;
	CPanel3 p3;
	CardLayout cl;

	@Override
	public void init() {
		getContentPane().add(mp = new JPanel());
		mp.setLayout(cl = new CardLayout());
		mp.add(p1 = new CPanel1(), "first");
		mp.add(p2 = new CPanel2(), "second");
		mp.add(p3 = new CPanel3(), "third");
		p1.prev.addActionListener(this);
		p1.next.addActionListener(this);
		p1.last.addActionListener(this);

		p2.first.addActionListener(this);
		p2.prev.addActionListener(this);
		p2.next.addActionListener(this);
		p2.last.addActionListener(this);

		p3.prev.addActionListener(this);
		p3.next.addActionListener(this);
		p3.first.addActionListener(this);

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object obj = arg0.getSource();
		if (obj == p1.prev) {
			cl.previous(mp);
		}
		if (obj == p1.next) {
			cl.next(mp);
		}
		if (obj == p1.last) {
			cl.last(mp);
		}
		if (obj == p2.first) {
			cl.first(mp);
		}
		if (obj == p2.prev) {
			cl.previous(mp);
		}
		if (obj == p2.next) {
			cl.next(mp);
		}
		if (obj == p2.last) {
			cl.last(mp);
		}
		if (obj == p3.prev) {
			cl.previous(mp);
		}
		if (obj == p3.next) {
			cl.next(mp);
		}
		if (obj == p3.first) {
			cl.first(mp);
		}
	}

}

// panel1
class CPanel1 extends JPanel {
	JLabel l1;
	JButton prev, next, last;

	public CPanel1() {
		super();
		setLayout(new FlowLayout());
		setBackground(Color.cyan);
		add(l1 = new JLabel("This is panel one"));
		l1.setFont(new Font("Vardena", Font.BOLD, 20));
		add(prev = new JButton("Previous"));
		add(next = new JButton("Next"));
		add(last = new JButton("Last"));
	}
}

// panel 2
class CPanel2 extends JPanel {
	JLabel l1;
	JButton first, prev, next, last;

	public CPanel2() {
		super();
		setLayout(new FlowLayout());
		setBackground(Color.YELLOW);
		add(l1 = new JLabel("This is panel two"));
		l1.setFont(new Font("Vardena", Font.BOLD, 20));
		add(first = new JButton("First"));
		add(prev = new JButton("Previous"));
		add(next = new JButton("Next"));
		add(last = new JButton("Last"));
	}
}

// panel 3
class CPanel3 extends JPanel {
	JLabel l1;
	JButton first, prev, next, last;

	public CPanel3() {
		super();
		setLayout(new FlowLayout());
		setBackground(Color.LIGHT_GRAY);
		add(l1 = new JLabel("This is panel three"));
		l1.setFont(new Font("Vardena", Font.BOLD, 20));
		add(first = new JButton("First"));
		add(prev = new JButton("Previous"));
		add(next = new JButton("Next"));
		add(last = new JButton("Last"));
	}
}

Download

Layout Manager in Java

Border Layout
Box Layout
Card Layout
Flow Layout
Gridbag Layout
Gridbag Layout
Grid Layout
Design GUI contianer without layout


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code