0

I'm attempting to code a GUI from scratch using NetBeans IDE 8.2. I have decided to base my RPG game on the Card Layout. So the layout starts on the card "Info Pane", and I can get the panels to change on the first switch, but I'm not quite sure how to get them to swap back. I have the program printing a string to the console so I can make sure the Cards switch properly.

package guigamefromscratch;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIGameFromScratch implements ItemListener{
    static String[] Classes = {
            "Barbarian",
            "Fighter",
            "Rogue",
            "Wizard"
        };
    static String[] Races = {
            "Dwarf",
            "Gnome",
            "Half-Orc",
            "Human"
        };
    static String[] Gender = {
            "Male",
            "Female"
        };
    static String cls;
    static String rcs;
    static String gen;
    static String gameName = "RPG Text";
    static Component frame = null;
    static Component icon = null;
    static JPanel cards;
    static GUIGameFromScratch work = new GUIGameFromScratch();
    static JPanel card1;

public static void main(String[] args) {
    Classes(Classes);
    Races(Races);
    Gender(Gender);
    work.Run();
}

public static void Classes(String[] Classes){
    cls = (String)JOptionPane.showInputDialog(frame,
            "Choose a Class:",
            gameName,
            JOptionPane.PLAIN_MESSAGE, (Icon) icon,
            Classes,
            Classes[0]
    );
}

public static void Races(String[] Races){
    rcs = (String)JOptionPane.showInputDialog(frame,
            "Choose a Race:",
            gameName,
            JOptionPane.PLAIN_MESSAGE, (Icon) icon,
            Races,
            Races[0]
    );
}

public static void Gender(String[] Gender){
    gen = (String)JOptionPane.showInputDialog(frame,
            "Choose a Gender:",
            gameName,
            JOptionPane.PLAIN_MESSAGE, (Icon) icon,
            Gender,
            Gender[0]
    );
}

public void Run(){
    JFrame display = new JFrame();
    display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    display.pack();
    display.setSize(300,250);
    display.setTitle(gameName);
    display.setVisible(true);
    display.setResizable(false);
    JPanel comboBoxPane = new JPanel();
    String[] comboBoxItems = {"Info Pane", "Game Pane"};
    JComboBox cb = new JComboBox(comboBoxItems);
    cb.setEditable(false);
    cb.addItemListener(this);
    comboBoxPane.add(cb);
    card1 = new JPanel();
    card1.add(new JLabel("Class: " + cls));
    card1.add(new JLabel("Race: " + rcs));
    card1.add(new JLabel("Gender: " + gen));
    cards = new JPanel(new CardLayout());
    cards.add(card1, comboBoxItems[0]);
    display.add(comboBoxPane, BorderLayout.PAGE_START);
    display.add(cards, BorderLayout.CENTER);
    comboBoxPane.setVisible(true);
    cards.setVisible(true);
    card1.setVisible(true);
}

@Override
public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() == ItemEvent.SELECTED){
        cards.setVisible(false);
        card1.setVisible(false);
        System.out.println("This is America");
        cards.revalidate();
        card1.revalidate();
        cards.repaint();
        card1.repaint();
    }
    else {
        cards.setVisible(true);
        card1.setVisible(true);
    }
}
}

If anyone has any help I would greatly appreciate it.

CurlyCue
  • 35
  • 1
  • 2
  • 1
    Start by understanding the `CardLayout` seen in [this answer](http://stackoverflow.com/a/5786005/418556). Then if you're still stuck, prepare a [mcve] based on a simplified example. E.G. the linked code is under 50 lines. – Andrew Thompson Aug 25 '18 at 02:11
  • 2
    Start by reading the Swing tutorial on [How to Use CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). Not only does the tutorial contain code to download and execute but it contains details about what the code does, I would also use the code from the tutorial to help you design your classes better. You should NOT be using static variables and methods. Also, method names and variable names should NOT start with an upper case character. Your code is hard to read because the forum highlights your variable like a class name. – camickr Aug 25 '18 at 02:13

0 Answers0