The CardLayout class manages the components in such a way that only one component is visible at a time. It treats each component as a card in the container. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.
Constructors:
- CardLayout(): It is used to create a new card layout with gaps of size is zero.
- CardLayout(int horizontalgap, int verticalgap): It is used to create a new CardLayout class with the specified horizontal and vertical gaps.
Commonly Used Methods:
- getLayoutAlignmentX(Container parent): Returns the alignment along the x-axis.
- getLayoutAlignmentY(Container parent): Returns the alignment along the y-axis.
- getVgap(): Used to get the vertical gap between components.
- addLayoutComponent(Component cm, Object cn): Used to add the specified component to this card layout’s internal table of names.
- getHgap(): Used to get the horizontal gap between components.
- toString(): Returns a string representation of the state of this card layout.
- removeLayoutComponent(Component cm): Used to remove the specified component from the layout.
Below programs illustrate the CardLayout class:
- Program 1: In the below program we are arranging several JLabel components in a JFrame, whose instance class is “Cardlayout“. We create 3 JButton components named “bt1“, “bt2“, “bt3” and then add them to the JFrame by the using add() method. We set the size and visibility of the frame by method setSize() and setVisible(). The layout is set by the method setLayout() method.
Tóm Tắt
Java
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.JFrame;
import
javax.swing.*;
public
class
Cardlayout
extends
JFrame
implements
ActionListener {
CardLayout card;
JButton b1, b2, b3;
Container c;
Cardlayout()
{
c = getContentPane();
card =
new
CardLayout(
40
,
30
);
c.setLayout(card);
b1 =
new
JButton(
"GEEKS"
);
b2 =
new
JButton(
"FOR"
);
b3 =
new
JButton(
"GEEKS"
);
b1.addActionListener(
this
);
b2.addActionListener(
this
);
b3.addActionListener(
this
);
c.add(
"a"
, b1);
c.add(
"b"
, b2);
c.add(
"c"
, b3);
}
public
void
actionPerformed(ActionEvent e)
{
card.next(c);
}
public
static
void
main(String[] args)
{
Cardlayout cl =
new
Cardlayout();
cl.setSize(
400
,
400
);
cl.setVisible(
true
);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output:
- Program 2: In below program we are arranging 4 JLabel components in a JFrame, whose class “CardlayoutDemo“. We create 4 JButton components named “firstbtn“, “nextbtn“, “previousbtn“, “lastbtn” and 4 JLabel components named “jl1“, “jl2“, “jl3” “jl4“. Here we are also creating 4 JPanel components named as “jp1“, “jp2“, “jp3“, “jp4” and then add them to the JFrame by the using add() method. We will set the size, visibility and title of the frame by using setSize(), setVisible() and setTitle() method respectively. The layout is set by using setLayout() method.
Java
import
java.awt.*;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
javax.swing.*;
public
class
CardLayoutDemo
extends
JFrame {
private
int
currentCard =
1
;
private
CardLayout cl;
public
CardLayoutDemo()
{
setTitle(
"Card Layout Example"
);
setSize(
300
,
150
);
JPanel cardPanel =
new
JPanel();
cl =
new
CardLayout();
cardPanel.setLayout(cl);
JPanel jp1 =
new
JPanel();
JPanel jp2 =
new
JPanel();
JPanel jp3 =
new
JPanel();
JPanel jp4 =
new
JPanel();
JLabel jl1 =
new
JLabel(
"Card1"
);
JLabel jl2 =
new
JLabel(
"Card2"
);
JLabel jl3 =
new
JLabel(
"Card3"
);
JLabel jl4 =
new
JLabel(
"Card4"
);
jp1.add(jl1);
jp2.add(jl2);
jp3.add(jl3);
jp4.add(jl4);
cardPanel.add(jp1,
"1"
);
cardPanel.add(jp2,
"2"
);
cardPanel.add(jp3,
"3"
);
cardPanel.add(jp4,
"4"
);
JPanel buttonPanel =
new
JPanel();
JButton firstBtn =
new
JButton(
"First"
);
JButton nextBtn =
new
JButton(
"Next"
);
JButton previousBtn =
new
JButton(
"Previous"
);
JButton lastBtn =
new
JButton(
"Last"
);
buttonPanel.add(firstBtn);
buttonPanel.add(nextBtn);
buttonPanel.add(previousBtn);
buttonPanel.add(lastBtn);
firstBtn.addActionListener(
new
ActionListener()
{
public
void
actionPerformed(ActionEvent arg0)
{
cl.first(cardPanel);
currentCard =
1
;
}
});
lastBtn.addActionListener(
new
ActionListener()
{
public
void
actionPerformed(ActionEvent arg0)
{
cl.last(cardPanel);
currentCard =
4
;
}
});
nextBtn.addActionListener(
new
ActionListener()
{
public
void
actionPerformed(ActionEvent arg0)
{
if
(currentCard <
4
)
{
currentCard +=
1
;
cl.show(cardPanel,
""
+ (currentCard));
}
}
});
previousBtn.addActionListener(
new
ActionListener()
{
public
void
actionPerformed(ActionEvent arg0)
{
if
(currentCard >
1
) {
currentCard -=
1
;
cl.show(cardPanel,
""
+ (currentCard));
}
}
});
getContentPane().add(cardPanel, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
public
static
void
main(String[] args)
{
CardLayoutDemo cl =
new
CardLayoutDemo();
cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cl.setVisible(
true
);
}
}
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html
My Personal Notes
arrow_drop_up