CardLayout as Layout Manager in Java

This tutorial is about the CardLayout as Layout Manager in Java. A CardLayout as a layout manager is used as a container of two or more components where it is represented as a card. Thus, there is only one card is visible at a time, and the container acts as a storage of cards. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of cardLayout.java. 2. Import following package library:
  1. import java.awt.*; //use to access the CardLayout class
  2. import java.awt.event.*; //used to access the ActionEvent and ActionListener class
  3. import javax.swing.*; //used to access JWindow, JPanel, and JButton class
3. We will extend a JPanel and implement an ActionListener of the button into the class name. Initialize your variable in your Main, variable card for the CardLayout. Have this code below:
  1. CardLayout card = new CardLayout(40, 40); // Create layout
As you can see above, the two parameters inside the CardLayout were the width and the height of the card layout. 40 is the width, and so as the height. 4. Now, we will make a constructor. We will set the layout into cardLayout using the setLayout method and will initialize a button.
  1.  setLayout(card);
  2.     JButton button;
Inside the constructor, we will create a looping and an ActionListener to access the other cards inside the button.
  1.     for (int i = 1; i <= 10; i++) {
  2.       add(button = new JButton(" Card " + i), "Card"); // Add a button
  3.       button.addActionListener(this); // Add listener for button
  4.     }
  5.   }
5. Now, we will handle the button events. There are five methods that are commonly used for the card layout. Namely, the next, previous, last, first, and show method.
  1.   public void actionPerformed(ActionEvent e) {
  2.     card.next(this); // Switch to the next card
  3.     card.previous(this); // Switch to the previous card
  4.     card.last(this); // Switch to the last card
  5.     card.first(this); // Switch to the first card
  6.   }
6. Now, code for your Main, initialize variable window for JWindow. Lastly,set the visibility and size, and add the CardLayout as the layout of the window. Have this code below:
  1.   public static void main(String[] args) {
  2.     JWindow window= new JWindow();
  3.     window.setSize(400,400);
  4.     window.getContentPane().add(new cardLayout());
  5.     window.setVisible(true);
  6.   }
Output: output output output Here's the full code of this tutorial:
  1. import java.awt.*; //use to access the CardLayout class
  2. import java.awt.event.*; //used to access the ActionEvent and ActionListener class
  3. import javax.swing.*; //used to access JWindow, JPanel, and JButton class
  4.  
  5. public class cardLayout extends JPanel implements ActionListener {
  6.   CardLayout card = new CardLayout(40, 40); // Create layout
  7.  
  8.   public cardLayout() {
  9.     setLayout(card);
  10.     JButton button;
  11.    
  12.     for (int i = 1; i <= 10; i++) {
  13.       add(button = new JButton(" Card " + i), "Card"); // Add a button
  14.       button.addActionListener(this); // Add listener for button
  15.     }
  16.   }
  17.   // Handle button events
  18.   public void actionPerformed(ActionEvent e) {
  19.     card.previous(this); // Switch to the previous card
  20.   }
  21.  
  22.   public static void main(String[] args) {
  23.     JWindow window= new JWindow();
  24.     window.setSize(400,400);
  25.     window.getContentPane().add(new cardLayout());
  26.     window.setVisible(true);
  27.   }
  28. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment