java - 我在类里面不断遇到初始化异常

标签 java

我正在使用 Java 制作一个基于文本的游戏,但遇到了问题。

我有第一个类“Game”和第二个类“ForestEvents”,我试图从 Game 类中的 ForestEvents 类调用方法 (townGate),同时引用 Game 类 () 中的变量

public class Game
{
TitleScreenHandler tsHandler = new TitleScreenHandler();

ChoiceHandler choiceHandler = new ChoiceHandler();

ComponentHandler compHandler = new ComponentHandler();

ForestEvents fEvents = new ForestEvents();


JFrame window; 
Container con;
JPanel titlePanel , startPanel, mainTextPanel, choiceButtonPanel, playerPanel;
JLabel titleLabel, hpLabel, hpLabelNumber, weaponLabel, weaponLabelName;
JButton startButton, choice1,choice2,choice3,choice4;
JTextArea mainTextArea;

Font titleFont = new Font("Times New Roman", Font.PLAIN, 100);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 30);


String playerName;
static String weapon,position;

static int playerHP;
int weaponDamage;

public static void main(String[] args)
{
    new Game();

}

public Game()
{
    window = new JFrame();
    window.setSize(1280,800);
    window.setTitle("W: " + window.getWidth() + " H: " + window.getHeight());
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setBackground(Color.black);
    window.setLayout(null);
    con = window.getContentPane();

    //Panels are used to make sections in the window (Background)
    //Labels are used to write in the sections/panels (Foreground)
    //Buttons can be pressed inside Panels

    //To make a text you need to design a panel, design its size/color,
    //Design its text the same way, then add it to the panel

    titlePanel = new JPanel();
    titlePanel.setBounds(100, 100,  1080, 150);
    titlePanel.setBackground(Color.black);      

    titleLabel = new JLabel("Adventure");
    titleLabel.setForeground(Color.white);
    titleLabel.setFont(titleFont);

    startPanel = new JPanel();
    startPanel.setBounds(540, 600, 200, 100);
    startPanel.setBackground(Color.black);

    startButton = new JButton("START");
    startButton.setBackground(Color.black);
    startButton.setForeground(Color.white);
    startButton.setFont(normalFont);
    startButton.addActionListener(tsHandler);

    titlePanel.add(titleLabel);
    startPanel.add(startButton);
    con.add(titlePanel);
    con.add(startPanel);

    window.setVisible(true);

}

public void createGameScreen()
{
    titlePanel.setVisible(false);
    startButton.setVisible(false);

    mainTextPanel = new JPanel();
    mainTextPanel.setBounds(100, 100, 1080, 250);
    mainTextPanel.setBackground(Color.black);

    mainTextArea = new JTextArea("This is the main text area");
    mainTextArea.setBounds(100,100,1080,250);
    mainTextArea.setBackground(Color.black);
    mainTextArea.setForeground(Color.white); 
    mainTextArea.setFont(normalFont);
    mainTextArea.setLineWrap(true);

    playerPanel = new JPanel();
    playerPanel.setBounds(100, 20, 1080, 50);
    playerPanel.setBackground(Color.blue);
    playerPanel.setLayout(new GridLayout(1,4));

    choiceButtonPanel = new JPanel();
    choiceButtonPanel.setBounds(500, 350, 300, 250);
    choiceButtonPanel.setLayout(new GridLayout(4,1));
    choiceButtonPanel.setBackground(Color.black);

    choice1 = new JButton("Choice 1");
    choice1.setBackground(Color.black);
    choice1.setForeground(Color.white);
    choice1.setFont(normalFont);
    choice1.setFocusPainted(false);
    choice1.addActionListener(choiceHandler);
    choice1.setActionCommand("c1");

    choiceButtonPanel.add(choice1);


    choice2 = new JButton("Choice 2");
    choice2.setBackground(Color.black);
    choice2.setForeground(Color.white);
    choice2.setFont(normalFont);
    choice2.setFocusPainted(false);
    choice2.addActionListener(choiceHandler);
    choice2.setActionCommand("c2");

    choiceButtonPanel.add(choice2);


    choice3 = new JButton("Choice 3");
    choice3.setBackground(Color.black);
    choice3.setForeground(Color.white);
    choice3.setFont(normalFont);
    choice3.setFocusPainted(false);
    choice3.addActionListener(choiceHandler);
    choice3.setActionCommand("c3");

    choiceButtonPanel.add(choice3);


    choice4 = new JButton("Choice 4");
    choice4.setBackground(Color.black);
    choice4.setForeground(Color.white);
    choice4.setFont(normalFont);
    choice4.setFocusPainted(false);
    choice4.addActionListener(choiceHandler);
    choice4.setActionCommand("c4");

    choiceButtonPanel.add(choice4);


    con.add(mainTextPanel);
    con.add(choiceButtonPanel);
    con.add(playerPanel);

    hpLabel = new JLabel("HP: ");
    hpLabel.setFont(normalFont);
    hpLabel.setForeground(Color.white);

    hpLabelNumber = new JLabel();
    hpLabelNumber.setFont(normalFont);
    hpLabelNumber.setForeground(Color.white);

    weaponLabel = new JLabel("Weapon: ");
    weaponLabel.setFont(normalFont);
    weaponLabel.setForeground(Color.white);

    weaponLabelName = new JLabel();
    weaponLabelName.setFont(normalFont);
    weaponLabelName.setForeground(Color.white);


    playerPanel.add(hpLabel);

    playerPanel.add(hpLabelNumber);

    playerPanel.add(weaponLabel);

    playerPanel.add(weaponLabelName);

    mainTextPanel.add(mainTextArea);

    playerSetup();

}

public void playerSetup()
{
    playerHP = 15;
    weapon = "Fists";
    weaponLabelName.setText(weapon);
    hpLabelNumber.setText("" + playerHP);

    fEvents.townGate();
}

public void townGate()
{
    position = "towngate";

    mainTextArea.setText("You are at the gates of the town. A guard is standing in front of you. What do you do?");
    choice1.setText("Talk to the Guard");
    choice2.setText("Attack the Guard");
    choice3.setText("Leave");
    choice4.setText("");
}


public void talkGuard()
{
    position = "talkguard";

    mainTextArea.setText("Guard: Hello Stranger. I have never seen you before. I'm sorry but I cannot let you enter.");

    choice1.setText("Go Back");
    choice2.setText("");
    choice3.setText("");
    choice4.setText("");

}

public void attackGuard()
{
    position = "attackguard";

    mainTextArea.setText("Guard: HOW DARE YOU! \nThe guard fought back and hit you hard.\n(You received 3 damage)");
    playerHP -= 3;
    hpLabelNumber.setText("" + playerHP);

    choice1.setText("Go Back");
    choice2.setText("");
    choice3.setText("");
    choice4.setText("");

}

public void crossRoad()
{
    position = "crossroads";

    mainTextArea.setText("You are at the crossroad.\n Go south to go back to the town.");

    choice1.setText("Go North");
    choice2.setText("Go East");
    choice3.setText("Go South");
    choice4.setText("Go West");

}

public class ComponentHandler implements ComponentListener
{
    public void componentResized(ComponentEvent e)
    {
        Component c = (Component)e.getSource();
        window.setTitle("W: " + c.getWidth() + " H: " + c.getHeight());
    }

    @Override
    public void componentHidden(ComponentEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void componentMoved(ComponentEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void componentShown(ComponentEvent e)
    {
        // TODO Auto-generated method stub

    }
}


public class TitleScreenHandler implements ActionListener 
{
    public void actionPerformed(ActionEvent event)
    {
        createGameScreen();
    }
}

public class ChoiceHandler implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        String yourChoice = event.getActionCommand(); 

        switch (position) 
        {
            case "towngate":
                switch(yourChoice)
                {
                case "c1": 
                    talkGuard();
                    break;
                case "c2": 
                    attackGuard();
                    break;
                case "c3": 
                    crossRoad();
                    break;
                case "c4": 
                    break;
                }
                break;

            case "talkguard":
                switch(yourChoice)
                {
                case "c1":
                    fEvents.townGate();
                    break;
                }
                break;

            case "attackguard":
                switch(yourChoice)
                {
                case "c1":
                    fEvents.townGate();
                    break;  
                }
                break;

            case "crossroad":
                switch(yourChoice)
                {
                case"c1":
                    break;
                case"c2":
                    break;
                case"c3":
                    fEvents.townGate();
                    break;
                case"c4":
                    break;
                }
                break;


        }

    }

}
}

第二课

public class ForestEvents extends Game
{

public void townGate()
{
    position = "towngate";

    mainTextArea.setText("You are at the gates of the town. A guard is standing in front of you. What do you do?");
    choice1.setText("Talk to the Guard");
    choice2.setText("Attack the Guard");
    choice3.setText("Leave");
    choice4.setText("");
}
}

但是每当我运行该程序时,我都会收到一个异常:

Exception in thread "main" java.lang.StackOverflowError

at ForestEvents.(ForestEvents.java:2)

at Game.(Game.java:23)

我想做的是调用第一个类中的方法并将它们放入第二个类中,因此当我在第二个类中添加更多方法时,它们将比第一个类中的所有内容更容易管理。

我的问题是如何修复异常?或者有更有效的方法来做到这一点吗?

最佳答案

您的 ForestEvents 扩展了 Game - 因此每个 ForestEvent 内部都会有一个 ForestEvent 实例,并有一个 ForestEvent 实例s..

这就是导致 StackOverflow 错误的原因。

我建议您删除 ForestEvents 字段并将其用作 main() 中的局部变量。

这是保持类整洁的有效方法 - 但在这种情况下,您可能需要创建一个“Starter”类(而不是 Game 本身)并将 main 放入其中。

关于java - 我在类里面不断遇到初始化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44285003/

相关文章:

java - Gradle 使用正确版本的依赖项

java - 通过在 EOL 上拆分来从 String 获取 List<String> 的简单方法?

Java JPA 带 NULL 检查的内连接

java - 如何在Java中将复杂的字符串转换为列表/数组?

java - 使用 gradle 构建错误 : Could not find or load main class,

java - 正则表达式获取以问号结尾的句子

java - 更灵活的工厂

java - MP Android Chart-如何固定折线图中X轴值的个数?

java - 带字符串且无参数的方法

java - 性能:BufferedOutputStream 与 FileWriter