Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CardLayout中的交换卡问题_Java_Swing_User Interface_Netbeans_Cardlayout - Fatal编程技术网

Java CardLayout中的交换卡问题

Java CardLayout中的交换卡问题,java,swing,user-interface,netbeans,cardlayout,Java,Swing,User Interface,Netbeans,Cardlayout,我试图使用NetBeans IDE 8.2从头开始编写GUI。我决定将我的RPG游戏建立在纸牌布局的基础上。所以布局从卡片“信息窗格”开始,我可以在第一次切换时改变面板,但我不太确定如何让它们交换回来。我有一个程序打印一个字符串到控制台,这样我可以确保卡开关正确 package guigamefromscratch; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUIGameFrom

我试图使用NetBeans IDE 8.2从头开始编写GUI。我决定将我的RPG游戏建立在纸牌布局的基础上。所以布局从卡片“信息窗格”开始,我可以在第一次切换时改变面板,但我不太确定如何让它们交换回来。我有一个程序打印一个字符串到控制台,这样我可以确保卡开关正确

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);
    }
}
}

如果有人能帮忙,我将不胜感激

首先了解中的
卡片布局。然后,如果你仍然被卡住,根据一个简化的例子准备一份报告。例如,链接代码少于50行。请阅读上的Swing教程。本教程不仅包含要下载和执行的代码,还包含有关代码功能的详细信息,我还将使用本教程中的代码帮助您更好地设计类。您不应该使用静态变量和方法。此外,方法名和变量名不应以大写字符开头。您的代码很难阅读,因为论坛会像类名一样突出显示您的变量。