Search code examples
javaswingdebuggingmenucardlayout

Java- CardLayout how to switch cards from the card that is switching


I am trying to get familiar with CardLayout so I am making a mock game menu. This menu should have three buttons, but that layout part is easy.

So, what I want to do is start it with a menu with three buttons. The single player button should change what the user sees to a single button, which can change it back to the original menu.

I followed an example online and then applied the same methods to this. However, the menu itself is a card and it's where the command to change cards will come from, not a separate container.

Whenever I run this i get an error:

public class GameMenuCards extends JFrame{

private int currentCard = 1;
private JPanel cardPanel;
private CardLayout cl;
private GridBagConstraints gbc;

public GameMenuCards(){
    initUI();
}

public void initUI(){

    //set the properties for the window
    setTitle("Game Menu With Cards");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setExtendedState(MAXIMIZED_BOTH);

    cardPanel = new JPanel();
    cl = new CardLayout();

    JPanel game = new JPanel();
    game.setBackground(Color.BLACK);

    //the menu panel
    JPanel menu = new JPanel();
    menu.setLayout(new GridBagLayout());
    menu.setBackground(Color.BLACK);

    cardPanel.add(menu, "1");
    cardPanel.add(game, "2");

    //set up the buttons for the menu
    JButton single = new JButton("Single Player");
    single.setPreferredSize(new Dimension(300, 30));
    single.setBackground(Color.GRAY);
    single.setForeground(Color.CYAN);
    single.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));

    JButton multi = new JButton("Multi Player");
    multi.setPreferredSize(new Dimension(300, 30));
    multi.setBackground(Color.GRAY);
    multi.setForeground(Color.CYAN);
    multi.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));

    JButton score = new JButton("High Scores");
    score.setPreferredSize(new Dimension(300, 30));
    score.setBackground(Color.GRAY);
    score.setForeground(Color.CYAN);
    score.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));

    gbc = new GridBagContraints();

    //add everything to the menu
    gbc.insets = new Insets(35, 50, 0, 50);

    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.gridx = 1;
    gbc.gridy = 1;

    menu.add(single, gbc);

    gbc.gridx = 1;
    gbc.gridy = 2;

    label(menu);

    gbc.gridx = 1;
    gbc.gridy = 3;

    menu.add(multi, gbc);

    gbc.gridx = 1;
    gbc.gridy = 4;

    label(menu);

    gbc.gridx = 1;
    gbc.gridy = 5;
    gbc.insets = new Insets(35, 50, 35, 50);

    menu.add(score, gbc);

    single.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            currentCard = 2;
            cl.show(cardPanel, "" + (currentCard));
        }
    });

    JButton returnBut = new JButton("Back To Menu");
    returnBut.setPreferredSize(new Dimension(300, 30));
    returnBut.setBackground(Color.GRAY);
    returnBut.setForeground(Color.CYAN);
    returnBut.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));

    returnBut.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            currentCard = 1;
            cl.show(cardPanel, "" + (currentCard));
        }
    });

    game.add(returnBut);

    getContentPane().add(cardPanel);

}

public void label(Container c){
    JLabel j1 = new JLabel();
    j1.setPreferredSize(new Dimension(300, 40));
    j1.setBackground(Color.BLACK);
    c.add(j1, gbc);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            GameMenuCards gm = new GameMenuCards();
            gm.setVisible(true);
        }
    });

}


}

I know that i could have done a similar thing with the buttons to the labels but i only had the thought two buttons in, so at that stage it would have taken longer.

Am I going about this the right way? Can you correct any mistakes I've made in the code?


Solution

  • Whenever I run this i get an error

    Your application is throwing an NPE here

    gbc.insets = new Insets(35, 50, 0, 50);
    

    as you haven't initialized your GridBagConstraints gbc.

    Also, the reason that you see both panels side-by-side is that, even though you created a CardLayout, you neglect to use it for your cardPanel. Therefore you are still using the default FlowLayout of the JPanel. You could do:

    cardPanel = new JPanel(cl);