Showing posts with label what is card layout in java. Show all posts
Showing posts with label what is card layout in java. Show all posts

Saturday, 18 May 2019

Card Layout in java - Online Help

Card Layout in java:

A card layout in java is a layout manager which treats each component as a  card. Only one card is visible at a time and the container acts as a stack of the card. The components are arranged like a deck of the card such that only one of them is visible at a time. In order to use card layout, it is required to create an object of type "panel" that will hold the cards. All this card must also be the objects of type "panel".The layout manager of the panel must be set to the card layout.

The card layout class in java defines the following constructors:

CardLayout()   //first

CardLayout(int hor,int ver)   //second

The first constructor creates a default card layout. The second constructor creates a card layout in which hor and ver specify the horizontal and vertical space left between each component respectively.

Sample program for card layout in java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class cardley extends JFrame implements
ActionListener
{
Container c;
CardLayout card;
cardley()
{
c=getContentPane();
card=new CardLayout(50,50);
c.setLayout(card);
JButton b1=new JButton("first");
JButton b2=new JButton("second");
JButton b3=new JButton("third");
c.add("button",b1);
c.add("button",b2);
c.add("button",b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSEpublic void action performed(ActionEvent ae)
{
card.next(c);//when a button is clicked show the next card.
}
public static void main(String args[])
{
cardley ob=new cardley();
ob.setTitle("Card Layout...");
ob.setSize(600,250);
ob.setVisible(true);
}