Friday, 7 September 2012

Java Card Layout Tutorial


Introduction of Java Card Layout Tutorial


Programm

Save MyWindowAdapter.java

import java.awt.event.*;
public class MyWindowAdapter extends WindowAdapter
{
	public void windowClosing(WindowEvent we)
	{
		System.exit(0);
	}
}


Save MyFrame.java 
 
import java.awt.*;
class MyFrame extends Frame implements ActionListener
{
	Panel mainPanel, card1, card2, card3, card4, card5, card6;
	Panel buttonPanel;
	Button b1, b2, b3, b4, b5, b6, btnFirst, btnPrevious, btnNext, btnLast;
	CardLayout cl;
	
	MyFrame()
	{
		super("Card Layout Demo");
		setSize(600,400);
		addWindowListener(new MyWindowAdapter());
		addControls();
		setResizable(false);
		setVisible(true);
	}
	
	private void addControls()
	{
		buttonPanel = new Panel();
		
		b1 = new Button("Card 1");
		b2 = new Button("Card 2");
		b3 = new Button("Card 3");
		b4 = new Button("Card 4");
		b5 = new Button("Card 5");
		b6 = new Button("Card 6");
		
		btnFirst = new Button("First");
		btnPrevious = new Button("Previous");
		btnNext = new Button("Next");
		btnLast = new Button("Last");
		
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		b5.addActionListener(this);
		b6.addActionListener(this);
		
		btnFirst.addActionListener(this);
		btnPrevious.addActionListener(this);
		btnNext.addActionListener(this);
		btnLast.addActionListener(this);
		
		buttonPanel.add(b1);
		buttonPanel.add(b2);
		buttonPanel.add(b3);
		buttonPanel.add(b4);
		buttonPanel.add(b5);
		buttonPanel.add(b6);
		
		buttonPanel.add(btnFirst);
		buttonPanel.add(btnPrevious);
		buttonPanel.add(btnNext);
		buttonPanel.add(btnLast);
		
		add(buttonPanel, BorderLayout.NORTH);
		
		cl = new CardLayout();
		mainPanel = new Panel();
		mainPanel.setLayout(cl);
		
		card1 = new Panel();
		card1.setBackground(Color.RED);
		
		
		card2 = new Panel();
		card2.setBackground(Color.GREEN);
		
		
		card3 = new Panel();
		card3.setBackground(Color.BLUE);
		
		
		card4 = new Panel();
		card4.setBackground(Color.MAGENTA);
		
		
		card5 = new Panel();
		card5.setBackground(Color.YELLOW);
		
		
		card6 = new Panel();
		card6.setBackground(Color.CYAN);
		
		
		mainPanel.add("card1",card1);
		mainPanel.add("card2",card2);
		mainPanel.add("card3",card3);	
		mainPanel.add("card4",card4);
		mainPanel.add("card5",card5);
		mainPanel.add("card6",card6);
		
		add(mainPanel, BorderLayout.CENTER);
		
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		if (ae.getSource() == b1)
			cl.show(mainPanel,"card1");
			
		else if (ae.getSource() == b2)
			cl.show(mainPanel,"card2");
			
		else if (ae.getSource() == b3)
			cl.show(mainPanel,"card3");
			
		else if (ae.getSource() == b4)
			cl.show(mainPanel,"card4");
			
		else if (ae.getSource() == b5)
			cl.show(mainPanel,"card5");
			
		else if (ae.getSource() == b6)
			cl.show(mainPanel,"card6");
			
		else if (ae.getSource() == btnFirst)
			cl.first(mainPanel);
			
		else if (ae.getSource() == btnPrevious)
			cl.previous(mainPanel);
			
		else if (ae.getSource() == btnNext)
			cl.next(mainPanel);
		
		else if (ae.getSource() == btnLast)
			cl.last(mainPanel);
	}		
}


Save GUI1.java

public class GUI1
{
	public static void main(String args[])
	{
		MyFrame mf = new MyFrame();
	}
}

No comments:

Post a Comment