0

I'm working on a group project and I'm the one making the GUI figuring it'd be good to practice with it. The program is supposed to be a pizza ordering system (pretty standard stuff) and what I'm trying to accomplish is that I have a main class that creates an application window. Inside this window is a panel that uses CardLayout with a button that when pressed calls another JPanel from another class dedicated specifically to that panel and places it as a card in the layout to be swapped back and forth from as normal.

What I have so far are the different panels I wish to call and the main class which has the window and main card panel. I can have it swap easily between panels created within the main class but when I try to use the panels from the other classes it just swaps to a blank panel when it should show the other class's panel.

The main class

package PizzaGUI;

import java.awt.EventQueue;
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;

public class PizzaSystem {

    private JFrame frame;
    Toppings toppingsPanel;
    MainMenu menuPanel;
    JButton loginBtn;
    JPanel mainPanel;
    CardLayout cl;
    

    //MAIN
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PizzaSystem window = new PizzaSystem();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //Constructor
    public PizzaSystem() {
        initialize();
    }
    
    //Initialize the GUI
    private void initialize() {
        
        cl = new CardLayout();
        
        frame = new JFrame();
        frame.setBounds(100, 100, 893, 527);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        mainPanel = new JPanel();
        mainPanel.setBounds(10, 10, 859, 470);
        frame.getContentPane().add(mainPanel);
        mainPanel.setLayout(cl);
        
        JPanel panel_2 = new JPanel();
        mainPanel.add(panel_2, "test");
        panel_2.setLayout(null);
        
        JLabel lblNewLabel = new JLabel("It Worked");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 45));
        lblNewLabel.setBounds(282, 118, 312, 103);
        panel_2.add(lblNewLabel);
        
        JPanel panel_1 = new JPanel();
        
        loginBtn = new JButton();
        loginBtn.setText("Login");
        loginBtn.setBounds(175, 72, 199, 154);
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showCard("menu");
            }
        });
        
        panel_1.add(loginBtn);
        
        mainPanel.add(panel_1, "loginPanel");
        JPanel menuPanel = new MainMenu();
        mainPanel.add(menuPanel, "menu");
        
        showCard("loginPanel");
        
    }
    
    //Call card matching the key
    public void showCard(String key) {
        cl.show(mainPanel, key);
    }
}

and one of the classes with the panel (format is messed up but should work still)

package PizzaGUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

public class MainMenu extends JPanel {

    public MainMenu() {
        
        JPanel panel1 = new JPanel();
        panel1.setBounds(100, 100, 893, 572);
        panel1.setBackground(Color.PINK);
        panel1.setLayout(null);
        panel1.setVisible(true);
        
        JLabel logoLabel = new JLabel("");
        logoLabel.setBounds(10, 10, 100, 110);
        panel1.add(logoLabel);
        ImageIcon image1 = new ImageIcon("C:\\Users\\thera\\eclipse-workspace\\PizzaSystem\\Images\\MamaJane1.png");
        logoLabel.setIcon(new ImageIcon("C:\\Users\\thera\\eclipse-workspace\\PizzaSystem\\Images\\MamaJane.png"));
        
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(133, 113, 548, 402);
        tabbedPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        tabbedPane.setBackground(Color.PINK);
        tabbedPane.setForeground(Color.GRAY);
        tabbedPane.setFont(new Font("Tahoma", Font.PLAIN, 20));
        tabbedPane.setToolTipText("");
        panel1.add(tabbedPane);
        
        JPanel panel = new JPanel();
        tabbedPane.addTab("MENU", null, panel, null);
        panel.setLayout(null);
        
        
        //*******************************************************************************************************
        //Pepperoni menu section
        JLabel pizza1Image = new JLabel("IMAGE");
        pizza1Image.setBounds(6, 25, 100, 100);
        panel.add(pizza1Image);
        ImageIcon image2 = new ImageIcon("C:\\Users\\thera\\eclipse-workspace\\PizzaSystem\\Images\\Pepperoni.jpg");
        Image pizza1 = image2.getImage();
        Image pepperoni = pizza1.getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH);
        image2 = new ImageIcon(pepperoni);
        pizza1Image.setIcon(image2);
        
        JLabel pepperoniLabel = new JLabel("PEPPERONI");
        pepperoniLabel.setHorizontalAlignment(SwingConstants.CENTER);
        pepperoniLabel.setBounds(110, 25, 86, 48);
        pepperoniLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(pepperoniLabel);
        
        
        JButton pepperoniOrderBtn = new JButton("ORDER");
        pepperoniOrderBtn.setBounds(110, 79, 86, 47);
        panel.add(pepperoniOrderBtn);
        pepperoniOrderBtn.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
            }
        });
        //Pepperoni End
        
        JButton accountButton = new JButton("Account");
        accountButton.setBounds(10, 414, 113, 39);
        accountButton.setBackground(Color.WHITE);
        panel.add(accountButton);
        
        JButton checkoutButton = new JButton("Checkout");
        checkoutButton.setBounds(713, 438, 138, 31);
        panel.add(checkoutButton);
        
        JButton logoutButton = new JButton("Logout");
        logoutButton.setBounds(10, 463, 113, 52);
        panel.add(logoutButton);
        
        JTextArea txtrOrderInfoGoes = new JTextArea();
        txtrOrderInfoGoes.setBounds(703, 10, 154, 418);
        txtrOrderInfoGoes.setText("Order Info\r\nGoes Here\r\n\r\nPepperoni Pizza x1\r\n$6.50");
        panel.add(txtrOrderInfoGoes);
        
        JButton clearOrderButton = new JButton("Clear");
        clearOrderButton.setBounds(723, 479, 113, 36);
        panel.add(clearOrderButton);
        
        JLabel titleLabel = new JLabel("MAMA JANE'S PIZZERIA");
        titleLabel.setBounds(147, 10, 534, 65);
        panel.add(titleLabel);
        titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.PLAIN, 50));
        
        JLabel storeInfoLabel = new JLabel("<html>1234 Fakelane Rd <br> 10001, Faketopia, USA <br> 111-11-PIZZA <br> Mon-Fri 11AM to 10pm <br> Sat-Sun 10AM to 11pm");
        storeInfoLabel.setBounds(10, 130, 113, 274);
        panel.add(storeInfoLabel);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(406, 85, 2, 2);
        panel.add(scrollPane);
        logoutButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

}


I can't tell where I've gone wrong and I've spent roughly the last three hours trying to fix this and searching the internet for answers to no avail so thank you in advance if you can help me out.

Also I apologize in advance, I know I end up misusing the proper terminology for programming alot, I understand what things are just forget what to properly call them sometimes.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user276019
  • 37
  • 8
  • Well, `frame.getContentPane().setLayout(null);`, is going to help you – MadProgrammer Jun 23 '21 at 01:24
  • You've not added anything to your `MainMenu` panel. Set the background color of the `MainMenu` panel and try running the code, you'll find that it switches, but because you've actually not added anything to it, it will show up as empty ... as expected – MadProgrammer Jun 23 '21 at 01:26
  • Avoid `null` layouts, they are far more trouble then they are worth. Take the time to look at [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) and figure out which layout managers will help you achieve the goals you need - you're not stuck to using one by the way – MadProgrammer Jun 23 '21 at 01:28
  • @MadProgrammer that is already in the code in the main class. Unless you think the frame is what I was trying to apply the cardlayout to, which the intent is to have the cardlayout on a panel attached to the frame and not the frame itself. – user276019 Jun 23 '21 at 01:28
  • No, inside `MainMenu`, everything is added to `panel`, but `panel` is never added to anything - but, you're use of `null` layouts is going to cause you no end of issues. Same thing with `panel1` .. what's it actually added to? – MadProgrammer Jun 23 '21 at 01:29
  • well the intent is to have mainmenu called as a panel and not a frame of panels. Additionally what do you mean I haven't added anything to my MainMenu panel? It's background is pink as it clearly says and I can run it independtly showing all the components I've put in it. – user276019 Jun 23 '21 at 01:30
  • So to specify against what you said, MainMenu has components in panel (they are not properly placed yet that is not the issue i have already fixed them), panel is part of a tabbedPane that is what it is attached to, not "everything" is added to panel. As for Panel1, it isn't added to anything because it as a whole is what I'm trying to get from that class as a panel to add to the cardLayout in PizzaSystem.java – user276019 Jun 23 '21 at 01:41
  • *"So to specify against what you said, MainMenu has components in panel "* - No, it doesn't - no where do you add `panel` or `panel1` to it, this is one reason why it's blank. The other is because you're not making use of appropriate layout managers. *"panel is part of a tabbedPane that is what it is attached to, not "everything" is added to panel."* - Nothing is added to `MainMenu`, which is the primary container you are displaying the via the `CardLayout`, so, nothing will be displayed – MadProgrammer Jun 23 '21 at 05:18
  • @MadProgrammer I'm pretty sure that in your first comment which reads *"is going to help you"* you meant *"is **not** going to help you"*. I could flag it for deletion, but hoping you can delete it soon. – Andrew Thompson Jul 05 '21 at 03:59
  • @user276019 If an answer helps you, please accept it. This is an important part of how this site works. – NomadMaker Jul 05 '21 at 05:31

1 Answers1

3

So, basically, I took out all the null layouts and "manual" layout code, as it's just going to mess with you to no end AND added add(panel1); to the end of the MainMenu constructor - as, I've said, NOTHING was added to MainMenu, so, nothing was going to get displayed.

Screen one

Screen Two

Before you tell me that "this isn't the layout I want", understand that I understand that, but my point is, null layouts are a really bad idea, as almost the entire Swing API relies the layout managers in one way or another.

I appreciate that layout management can seem like a complex subject, but it solves some very complex problems and it's worth taking the time to learn them. Remember, you're not stuck to a single layout manager, you can use component components to adjust individual containers to their individual needs.

You can take a look at:

to some ideas how you might approach designing a complex UI.

You should also take a look at Laying Out Components Within a Container

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

public class PizzaSystem {

    private JFrame frame;
//    Toppings toppingsPanel;
    MainMenu menuPanel;
    JButton loginBtn;
    JPanel mainPanel;
    CardLayout cl;

    //MAIN
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PizzaSystem window = new PizzaSystem();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //Constructor
    public PizzaSystem() {
        initialize();
    }

    //Initialize the GUI
    private void initialize() {

        cl = new CardLayout();

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainPanel = new JPanel(cl);
        frame.getContentPane().add(mainPanel);

        JPanel panel_1 = new JPanel(new GridBagLayout());

        loginBtn = new JButton();
        loginBtn.setText("Login");
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showCard("menu");
            }
        });

        panel_1.add(loginBtn);

        mainPanel.add(panel_1, "loginPanel");

        JPanel menuPanel = new MainMenu();
        mainPanel.add(menuPanel, "menu");

        frame.pack();

        showCard("loginPanel");

    }

    //Call card matching the key
    public void showCard(String key) {
        cl.show(mainPanel, key);
    }

    public class MainMenu extends JPanel {

        public MainMenu() {

            JPanel panel1 = new JPanel(new BorderLayout());

            JLabel logoLabel = new JLabel("Logo");
            panel1.add(logoLabel, BorderLayout.NORTH);

            JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
            tabbedPane.setToolTipText("");
            panel1.add(tabbedPane, BorderLayout.CENTER);

            JPanel panel = new JPanel();
            tabbedPane.addTab("MENU", null, panel, null);

            //*******************************************************************************************************
            //Pepperoni menu section
            JLabel pizza1Image = new JLabel("IMAGE");
            panel.add(pizza1Image);

            JLabel pepperoniLabel = new JLabel("PEPPERONI");
            pepperoniLabel.setHorizontalAlignment(SwingConstants.CENTER);
            panel.add(pepperoniLabel);

            JButton pepperoniOrderBtn = new JButton("ORDER");
            pepperoniOrderBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                }
            });
            //Pepperoni End

            JButton accountButton = new JButton("Account");
            panel.add(accountButton);

            JButton checkoutButton = new JButton("Checkout");
            panel.add(checkoutButton);

            JButton logoutButton = new JButton("Logout");
            panel.add(logoutButton);

            JTextArea txtrOrderInfoGoes = new JTextArea(10, 20);
            txtrOrderInfoGoes.setText("Order Info\r\nGoes Here\r\n\r\nPepperoni Pizza x1\r\n$6.50");
            panel.add(txtrOrderInfoGoes);

            JButton clearOrderButton = new JButton("Clear");
            panel.add(clearOrderButton);

            JLabel titleLabel = new JLabel("MAMA JANE'S PIZZERIA");
            panel.add(titleLabel);
            titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.PLAIN, 50));

            JLabel storeInfoLabel = new JLabel("<html>1234 Fakelane Rd <br> 10001, Faketopia, USA <br> 111-11-PIZZA <br> Mon-Fri 11AM to 10pm <br> Sat-Sun 10AM to 11pm");
            panel.add(storeInfoLabel);

            JScrollPane scrollPane = new JScrollPane();
            panel.add(scrollPane);
            logoutButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });

            add(panel1);
        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you for your help, sorry if I seemed frustrated before. I will try to add your fix using a proper layout. – user276019 Jun 24 '21 at 21:29