Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

Java Cardlayout: Constructors of Cardlayout Class

The CardLayout class manages components so that only one is visible at a time, treating each like a card. It has constructors to create layouts with or without gaps. Methods like next(), previous(), first(), and last() flip between cards, while show() displays a specific card by name. The example creates a CardLayout with buttons and uses action listeners and the next() method to transition between cards when the buttons are clicked.

Uploaded by

ramadevi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Java Cardlayout: Constructors of Cardlayout Class

The CardLayout class manages components so that only one is visible at a time, treating each like a card. It has constructors to create layouts with or without gaps. Methods like next(), previous(), first(), and last() flip between cards, while show() displays a specific card by name. The example creates a CardLayout with buttons and uses action listeners and the next() method to transition between cards when the buttons are clicked.

Uploaded by

ramadevi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java CardLayout

The CardLayout class manages the components in such a manner that only one component is
visible at a time. It treats each component as a card that is why it is known as CardLayout.

Constructors of CardLayout class

1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and
vertical gap.

Commonly used methods of CardLayout class

 public void next(Container parent): is used to flip to the next card of the given
container.
 public void previous(Container parent): is used to flip to the previous card of the given
container.
 public void first(Container parent): is used to flip to the first card of the given
container.
 public void last(Container parent): is used to flip to the last card of the given container.
 public void show(Container parent, String name): is used to flip to the specified card
with the given name.

Example of CardLayout class

1. import java.awt.*;  
2. import java.awt.event.*;  
3.   
4. import javax.swing.*;  
5.   
6. public class CardLayoutExample extends JFrame implements ActionListener{  
7. CardLayout card;  
8. JButton b1,b2,b3;  
9. Container c;  
10.     CardLayoutExample(){  
11.           
12.         c=getContentPane();  
13.         card=new CardLayout(40,30);  
14. //create CardLayout object with 40 hor space and 30 ver space  
15.         c.setLayout(card);  
16.           
17.         b1=new JButton("Apple");  
18.         b2=new JButton("Boy");  
19.         b3=new JButton("Cat");  
20.         b1.addActionListener(this);  
21.         b2.addActionListener(this);  
22.         b3.addActionListener(this);  
23.               
24.         c.add("a",b1);c.add("b",b2);c.add("c",b3);  
25.                           
26.     }  
27.     public void actionPerformed(ActionEvent e) {  
28.     card.next(c);  
29.     }  
30.   
31.     public static void main(String[] args) {  
32.         CardLayoutExample cl=new CardLayoutExample();  
33.         cl.setSize(400,400);  
34.         cl.setVisible(true);  
35.         cl.setDefaultCloseOperation(EXIT_ON_CLOSE);  
36.     }  
37. }  

You might also like