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 8013793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:43:45+00:00 2026-06-04T19:43:45+00:00

I know it’s something to do with how I’ve set it up and the

  • 0

I know it’s something to do with how I’ve set it up and the actionlistener not being correctly set to the frame or something but I just can’t get my hear around it. If someone could point me in the right direction I’d be much obliged. Sorry for noob question.

Here’s what I have:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Main implements ActionListener {

    JPanel cardHolder;
    public static final String HOME_CARD = "Home";
    public static final String BLUE_PANEL = "Blue Panel";
    public static final String RED_PANEL = "Red Panel";
    public static final String ORANGE_PANEL = "Orange Panel";

    public static JButton home = new JButton("Home");
    public static JButton bluePanel = new JButton("Blue Card");
    public static JButton redPanel = new JButton("Red Panel");
    public static JButton orangePanel = new JButton("Orange Panel");

    public static JPanel createCardHolderPanel() {
        JPanel cardHolder = new JPanel(new CardLayout());
        cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));

        cardHolder.add(createHomeCard(), HOME_CARD);
        cardHolder.add(createBluePanel(), BLUE_PANEL);
        cardHolder.add(createRedPanel(), RED_PANEL);
        cardHolder.add(createOrangePanel(), ORANGE_PANEL);

        return cardHolder;
    }

    private static JPanel createOrangePanel() {
        JPanel orangePanel = new JPanel();

        orangePanel.setBackground(Color.orange);
        return orangePanel;
    }

    private static Component createRedPanel() {
        JPanel redPanel = new JPanel();

        redPanel.setBackground(Color.red);
        return redPanel;
    }

    private static Component createBluePanel() {
        JPanel bluePanel = new JPanel();

        bluePanel.setBackground(Color.blue);
        return bluePanel;
    }

    private static Component createHomeCard() {
        JPanel homePanel = new JPanel();

        homePanel.setBackground(Color.GRAY);
        return homePanel;
    }

    public static JPanel createButtonPanel() {
        JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
        buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));

        buttonPanel.add(home);
        buttonPanel.add(bluePanel);
        buttonPanel.add(redPanel);
        buttonPanel.add(orangePanel);

        return buttonPanel;
    }

    public static JPanel createContentPane() {
        JPanel contentPane = new JPanel(new BorderLayout());

        contentPane.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
        contentPane.setBackground(Color.WHITE);
        contentPane.setPreferredSize(new Dimension(499, 288));

        contentPane.add(createButtonPanel(), BorderLayout.WEST);
        contentPane.add(createCardHolderPanel(),BorderLayout.CENTER);

        return contentPane;
    }

    public static JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        JMenu file = new JMenu("File");
        JMenu users = new JMenu("Users");
        JMenu options = new JMenu("Options");
        JMenu help = new JMenu("Help");

        menuBar.add(file);
        menuBar.add(users);
        menuBar.add(options);
        menuBar.add(help);

        return menuBar;
    }

    public static void createAndShowGUI() {
        JFrame frame = new JFrame("Simple CardLayout Program");
        frame.setContentPane(createContentPane());
        frame.setJMenuBar(createMenuBar());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == home) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            cardLayout.show(cardHolder, HOME_CARD);
        }

        if (e.getSource() == bluePanel) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            cardLayout.show(cardHolder, BLUE_PANEL);
        }

        if (e.getSource() == redPanel) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            cardLayout.show(cardHolder, RED_PANEL);
        }

        if (e.getSource() == orangePanel) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            cardLayout.show(cardHolder, ORANGE_PANEL);
        }
    }
}
  • 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-04T19:43:47+00:00Added an answer on June 4, 2026 at 7:43 pm

    Others have suggested listening to the buttons; in addition:

    • Prefer the lowest accessibility consistent with use, e.g. private rather than public.
    • Don’t make everything static.
    • Use static for immutable constants used throughout the class.
    • Use class variables rather than static members for content.
    • Don’t repeat your self, e.g. initialize cardLayout just once in your actionPerformed)().
    • Use parameters rather than separate methods for each color, e.g.

      private JPanel createColorPanel(Color color) {...}
      

    Revised code:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Main implements ActionListener {
    
        private static final String HOME_CARD = "Home";
        private static final String BLUE_PANEL = "Blue Panel";
        private static final String RED_PANEL = "Red Panel";
        private static final String ORANGE_PANEL = "Orange Panel";
        private JPanel cardHolder;
        private JButton homeButton = new JButton("Home");
        private JButton blueButton = new JButton("Blue Card");
        private JButton redButton = new JButton("Red Panel");
        private JButton orangeButton = new JButton("Orange Panel");
    
        public JPanel createCardHolderPanel() {
            cardHolder = new JPanel(new CardLayout());
            cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
            cardHolder.add(createColorPanel(Color.gray), HOME_CARD);
            cardHolder.add(createColorPanel(Color.blue), BLUE_PANEL);
            cardHolder.add(createColorPanel(Color.red), RED_PANEL);
            cardHolder.add(createColorPanel(Color.orange), ORANGE_PANEL);
    
            return cardHolder;
        }
    
        private JPanel createColorPanel(Color color) {
            JPanel panel = new JPanel();
            panel.setBackground(color);
            return panel;
        }
    
        public JPanel createButtonPanel() {
            JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
            buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
            buttonPanel.add(homeButton);
            buttonPanel.add(blueButton);
            buttonPanel.add(redButton);
            buttonPanel.add(orangeButton);
            homeButton.addActionListener(this);
            blueButton.addActionListener(this);
            redButton.addActionListener(this);
            orangeButton.addActionListener(this);
            return buttonPanel;
        }
    
        public JPanel createContentPane() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
            panel.setBackground(Color.WHITE);
            panel.setPreferredSize(new Dimension(499, 288));
            panel.add(createButtonPanel(), BorderLayout.WEST);
            panel.add(createCardHolderPanel(), BorderLayout.CENTER);
            return panel;
        }
    
        public JMenuBar createMenuBar() {
            JMenuBar menuBar = new JMenuBar();
            JMenu file = new JMenu("File");
            JMenu users = new JMenu("Users");
            JMenu options = new JMenu("Options");
            JMenu help = new JMenu("Help");
            menuBar.add(file);
            menuBar.add(users);
            menuBar.add(options);
            menuBar.add(help);
            return menuBar;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            if (e.getSource() == homeButton) {
                cardLayout.show(cardHolder, HOME_CARD);
            }
            if (e.getSource() == blueButton) {
                cardLayout.show(cardHolder, BLUE_PANEL);
            }
            if (e.getSource() == redButton) {
                cardLayout.show(cardHolder, RED_PANEL);
            }
            if (e.getSource() == orangeButton) {
                cardLayout.show(cardHolder, ORANGE_PANEL);
            }
        }
    
        public static void createAndShowGUI() {
            JFrame frame = new JFrame("Simple CardLayout Program");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Main main = new Main();
            frame.setJMenuBar(main.createMenuBar());
            frame.add(main.createContentPane());
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that it's a subject that can raise a lot of debate, but
I know that design patterns is generally something that's connected to OO programming, but
I know regex and substrings is a common question on here but i can
I know that there's something fishy about the malloc part, but I'm having trouble
I KNOW I did this in WP7 (not WP7.1) and I can't figure out
I know we already have many posts about this topic, but I just cannot
I know we can trigger an Intent.ACTION_SEND intent to send email. But according to
I know this question has been around, but I found answers a bit foggy,
I know that I can do something like $int = (int)99; //(int) has a
I know this question may be weird. But I just want to know whether

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.