Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:57:19+00:00 2026-06-18T06:57:19+00:00

I am gathering things for this KET test project. But one thing that really

  • 0

I am gathering things for this KET test project. But one thing that really bothers me is this page navigation I can’t figure out how to make it work properly. I am trying to use CardsLayout. But it dosen’t work properly. I need help how to set up those panes.

At the bottom I need to make question numbers, for start I think I will make only 5. When number is pressed it navigates to page which is marked by that number. Next and back buttons navigate +- one page and the result button takes to final page where test is sumed up.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:57:20+00:00Added an answer on June 18, 2026 at 6:57 am

    Look at the code give below.It is very basic program for setting and handling CardLayout and very much fulfill the requirement that you are looking for . I hope it would assist you better in understanding the use and handling of CardLayout.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class CardLayoutDemo extends JFrame implements ActionListener
    {
        JButton next;
        JButton prev;
        JButton[] buttons ;
        JPanel[]  cards;
        JPanel    centerPanel;//Going to add all cards
        JPanel lowerPanel;
        int currentButton = 0;
        public void prepareAndShowGUI() 
        {
            next      = new JButton("Next>>");
            prev      = new JButton("<<Previous");
            buttons   = new JButton[10];
            String[]  cardsName = {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};
            cards     = new JPanel[10];
            centerPanel= new JPanel();
            lowerPanel = new JPanel();
            centerPanel.setLayout(new CardLayout());
            for (int i = 0; i < buttons.length ; i++ )
            {
                buttons[i] = new JButton(cardsName[i]);
                buttons[i].addActionListener(this);
                buttons[i].setActionCommand("CARDS"+i);
                lowerPanel.add(buttons[i]);
                cards[i] = new JPanel();
                cards[i].setLayout(new BorderLayout());
                cards[i].setBorder(BorderFactory.createTitledBorder("Card "+(i+1)));
                cards[i].add(new JLabel(cardsName[i], SwingUtilities.CENTER));
                centerPanel.add(cards[i], cardsName[i]);
            }
            getContentPane().add(centerPanel);
            getContentPane().add(lowerPanel,BorderLayout.SOUTH);
            getContentPane().add(next,BorderLayout.EAST);
            getContentPane().add(prev,BorderLayout.WEST);
            next.addActionListener(this);
            prev.addActionListener(this);
            prev.setEnabled(false);
            if (buttons.length == 1)
            {
                next.setEnabled(false);
            }
            setSize(700,400);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent evt) 
        {
            JButton clickedButton = (JButton)evt.getSource();
            String command  = clickedButton.getActionCommand();
            if (command.startsWith("CARDS"))
            {
                CardLayout cl = (CardLayout)(centerPanel.getLayout());
                cl.show(centerPanel, clickedButton.getText());
                currentButton = Integer.parseInt(command.substring("CARDS".length()));
                if (currentButton == 0)
                {
                    prev.setEnabled(false);
                    if (buttons.length > 1)
                    {
                        next.setEnabled(true);
                    }
                }
                else if (currentButton == buttons.length - 1)
                {
                    next.setEnabled(false);
                    if (buttons.length > 1)
                    {
                        prev.setEnabled(true);
                    }
                }
                else
                {
                    if (buttons.length > 1)
                    {
                        next.setEnabled(true);
                        prev.setEnabled(true);
                    }
                }
            }
            else
            {
                if ("Next>>".equals(clickedButton.getText()))
                {
                    buttons[++currentButton].doClick();
                    if (currentButton == buttons.length - 1)
                    {
                        next.setEnabled(false);
                        prev.requestFocus();
                    }
                    prev.setEnabled(true);
                }
                else if ("<<Previous".equals(clickedButton.getText()))
                {
                    buttons[--currentButton].doClick();
                    if (currentButton == 0)
                    {
                        prev.setEnabled(false);
                        next.requestFocus();
                    }
                    next.setEnabled(true);
                }
            }
    
        }     
        public static void main(String[] args) 
        {
            SwingUtilities.invokeLater(new Runnable() 
            {
                public void run() 
                {
                    CardLayoutDemo cld = new CardLayoutDemo();
                    cld.prepareAndShowGUI();
                }
            });
        }
    }
    

    To know more about how to use CardLayout watch this official tutorial
    .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one process that's gathering data in real time and another process that's
If gathering requirements for a medium-to-large web-based project, at what point should one consider
This is a shameless information gathering exercise for my own book. One of the
Need some help gathering thoughts on this issue. Our team is moving ahead with
I'm interesting in gathering usage metrics for an application that I did not write
I've been gathering ideas for my personal project in CSS/JavaScript/XHTML. The idea is to
I need to build a content gathering program that will simply read numbers on
I am gathering small information regarding kernel dll functions.. and I found this function
i was in the process of doing a fun project gathering music info/playable links
I have an app that's gathering registration data (username/password, etc). I need to gather

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.