我正在使用Java制作基于文本的游戏,但我遇到了一个问题。
我有第一堂课"游戏"和第二课" 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("");
}
}
但每当我运行该程序时,我都会得到一个例外情况:
线程中的异常" main" java.lang.StackOverflowError的
在ForestEvents。(ForestEvents.java:2)
在游戏中。(Game.java:23)
我尝试做的是调用第1类中的方法并将它们放在2md类中,这样当我在第2类中添加更多方法时,它们将比第1类中的所有方法更容易管理。
我的问题是如何修复异常?或者有更有效的方法吗?
答案 0 :(得分:1)
您的ForestEvents extends Game
- 所以每个ForestEvent
内部都会有ForestEvent
个实例,其实例为ForestEvent
s ..
导致StackOverflow错误的原因。
我建议您删除字段ForestEvents并将其用作main()中的局部变量。
这是保持课堂整洁的有效方法 - 但在这种情况下,你可能想要创建一个" Starter" class(不是Game
本身)并将main放入其中。