M. Amadou
M. Amadou

Reputation: 15

CardLayout showing blank JPanel in Java

I've searched up different tutorials and looked at the Class Profile for CardLayout and JPanel but I can't seem to get my window to show up. Currently it opens a frame with the proper dimensions and title but nothing in the actual container.

This is the code I have(P.S. I know it's a hot mess)

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class Casino extends JFrame implements ActionListener {

    private JButton start, settings, scenario, music;


    /**
     * Constructor method
     */

    public Casino(){

        JPanel mainUI, startUI, settingsUI, scenarioUI, blackjackUI, oddorevenUI, tcmUI, overorunderUI, slotsUI;
        JPanel menus = new JPanel(new CardLayout());


        CardLayout GUI = (CardLayout) menus.getLayout();
        mainUI = new JPanel();
        getContentPane().add(mainUI);
        mainUI.setBackground(new Color(53, 9, 9));

        //Background items
        JLabel title = new JLabel(new ImageIcon("title.png"));
        title.setBounds(0,-280,780,700);
        mainUI.add(title);

        JLabel border = new JLabel(new ImageIcon("mainscreenborder.png"));
        border.setBounds(0, 180, 780, 700);
        mainUI.add(border);

        //Main menu buttons
        settings = new JButton();
        ImageIcon s = new ImageIcon("settings-button.png");
        settings.setBounds(320, 200, 122, 63);
        settings.setIcon(s);
        mainUI.add(settings);

        music = new JButton();
        ImageIcon m = new ImageIcon("music-button.png");
        music.setBounds(320, 268, 122, 63);
        music.setBackground(new Color(53, 9, 9));
        music.setIcon(m);
        mainUI.add(music);

        scenario = new JButton();
        ImageIcon sc = new ImageIcon("scenario-button.png");
        scenario.setBounds(320, 336, 122, 63);
        scenario.setBackground(new Color(53, 9, 9));
        scenario.setIcon(sc);
        mainUI.add(scenario);

        start = new JButton();
        ImageIcon st = new ImageIcon("start-button.png");
        start.setBounds(320, 404, 122, 63);
        start.setBackground(new Color(53, 9, 9));
        start.setIcon(st);
        mainUI.add(start);


        menus.add(mainUI, "Main Menu");

        GUI.show(menus, "Main Menu");




        setSize(780, 700);
        setResizable(false);
        setLayout(GUI);
        setTitle("White Lily Casino");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e){

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Casino wlc = new Casino();

    }
}

Note: It worked before when I was using the Container c method instead of using a JPanel and CardLayout. I am trying to switch it to card layout now because I want to use buttons to navigate to multiple screens

Upvotes: 0

Views: 478

Answers (1)

Esteban Rincon
Esteban Rincon

Reputation: 2110

Try adding the mainUI to the JFrame

getContentPane().add(mainUI)

or

add(mainUI)

Upvotes: 1

Related Questions