CardLayout as Layout Manager in Java
Submitted by donbermoy on Tuesday, December 2, 2014 - 21:59.
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:
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:
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.
Inside the constructor, we will create a looping and an ActionListener to access the other cards inside the button.
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.
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:
Here's the full code of this tutorial:
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
- import java.awt.*; //use to access the CardLayout class
- import java.awt.event.*; //used to access the ActionEvent and ActionListener class
- import javax.swing.*; //used to access JWindow, JPanel, and JButton class
- setLayout(card);
- JButton button;
- for (int i = 1; i <= 10; i++) {
- button.addActionListener(this); // Add listener for button
- }
- }
- card.next(this); // Switch to the next card
- card.previous(this); // Switch to the previous card
- card.last(this); // Switch to the last card
- card.first(this); // Switch to the first card
- }
Output:



- import java.awt.*; //use to access the CardLayout class
- import java.awt.event.*; //used to access the ActionEvent and ActionListener class
- import javax.swing.*; //used to access JWindow, JPanel, and JButton class
- public cardLayout() {
- setLayout(card);
- JButton button;
- for (int i = 1; i <= 10; i++) {
- button.addActionListener(this); // Add listener for button
- }
- }
- // Handle button events
- card.previous(this); // Switch to the previous card
- }
- window.setSize(400,400);
- window.getContentPane().add(new cardLayout());
- window.setVisible(true);
- }
- }
Add new comment
- 92 views