0

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

M. Amadou
  • 15
  • 4
  • I don't see you adding the panel, `getContentPane().add(mainUI)` – Esteban Rincon Jan 05 '16 at 22:30
  • Why are you using `setBounds()` instead of a combination of [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)? – Frakcool Jan 05 '16 at 22:30
  • 1
    I don't see where `menus` gets added to anything? – MadProgrammer Jan 05 '16 at 22:33
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jan 05 '16 at 22:34
  • what am I meant to add menus too? – M. Amadou Jan 05 '16 at 22:34
  • @MadProgrammer I pasted the code that I'm currently running – M. Amadou Jan 05 '16 at 22:40
  • @M.Amadou Add `menus` to whatever container you want ti to show up on – MadProgrammer Jan 05 '16 at 22:42
  • Thank you that worked :) and Side Question: Can I not use setBounds for JPanels? because the button's aren't the correct size and they aren't showing up in the correct locations – M. Amadou Jan 05 '16 at 22:48
  • As I said avoid the use of `setBounds`, that means you're using a null layout, see [here](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) and [here](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html) for more information on why you should avoid it. – Frakcool Jan 05 '16 at 23:10

1 Answers1

1

Try adding the mainUI to the JFrame

getContentPane().add(mainUI)

or

add(mainUI)
Esteban Rincon
  • 2,040
  • 3
  • 27
  • 44
  • getContentPane().add(mainUI) didn't work but add(mainUI) did(in terms of showing the content). How come everything is not in its place set by setBounds? Is there a similar method to setBounds for JPanels that I should have used? – M. Amadou Jan 05 '16 at 22:42
  • you use the `setBounds()` method when you are using **absolute** positioning, and this only works if you set the layout of your container to `null` – Esteban Rincon Jan 05 '16 at 22:47
  • If you want your components to work with bounds then `mainUI.setLayout(null);` instead of using a card layout. this will show everything in its place by setBounds – Esteban Rincon Jan 05 '16 at 22:49