堆栈溢出错误

时间:2012-10-23 16:29:52

标签: java stack-overflow

当我运行这个我制作的短程序时,我不断收到堆栈溢出错误!请帮忙!现在它应该做的就是获取用户输入并打印它们的位置(在X和Y坐标中)。我不确定Stack溢出错误是什么或如何解决它。

import java.awt.*;
import javax.swing.*;

public class ExplorerPanel extends JFrame {

    ExplorerEvent prog = new ExplorerEvent(this);

    JTextArea dataa = new JTextArea(15, 20);
    JTextField datain = new JTextField(20);
    JButton submit = new JButton("Submit");
    JTextField errors = new JTextField(30);

    public ExplorerPanel() {
        super("Explorer RPG");
        setLookAndFeel();
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_…
        BorderLayout bord = new BorderLayout();
        setLayout(bord);

        JPanel toppanel = new JPanel();
        toppanel.add(dataa);
        add(toppanel, BorderLayout.NORTH);

        JPanel middlepanel = new JPanel();
        middlepanel.add(datain);
        middlepanel.add(submit);
        add(middlepanel, BorderLayout.CENTER);

        JPanel bottompanel = new JPanel(); 
        bottompanel.add(errors);
        add(bottompanel, BorderLayout.SOUTH);

        dataa.setEditable(false);
        errors.setEditable(false);

        submit.addActionListener(prog);

        setVisible(true);
    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                                   "com.sun.java.swing.plaf.nimbus.NimbusLo…
            );
        } catch (Exception exc) {
            // ignore error
        }
    }

    public static void main(String[] args) {
        ExplorerPanel frame = new ExplorerPanel();
    }

}

public class ExplorerEvent implements ActionListener, Runnable {

    ExplorerPanel gui;
    Thread playing;
    String command;
    String gamedata;
    ExplorerGame game = new ExplorerGame();

    public ExplorerEvent(ExplorerPanel in) {
        gui = in;
    }

    public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        if (command.equals("Submit")) {
            startPlaying();
        }
    }

    void startPlaying() {
        playing = new Thread(this);
        playing.start();
    } 

    void stopPlaying() {
        playing = (null);
    }

    void clearAllFields() {
        gui.dataa.setText("");
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (playing == thisThread) {

            // Game code
            game.Game();

        }
    }

}

public class ExplorerGame {

    ExplorerPanel gui = new ExplorerPanel();
    String command;
    int x = 1;
    int y = 1;

    public void Game() {
        command = gui.submit.getText().toLowerCase();

        if (command.equals("east")) {x--;}
        else if (command.equals("south")) {y--;}
        else if (command.equals("west")) {x++;}
        else if (command.equals("north")) {y++;}
        System.out.println(x + y);
    }
}

1 个答案:

答案 0 :(得分:6)

ExplorerGame 中,您已声明: -

ExplorerPanel gui = new ExplorerPanel();

然后,在 ExplorerPanel 中: -

ExplorerEvent prog = new ExplorerEvent(this);

然后再次在 ExplorerEvent 中: -

ExplorerGame game = new ExplorerGame();

这将通过递归创建对象来填充堆栈。

ExplorerGame -> ExplorerPanel -> ExplorerEvent --+
    ^                                            |
    |____________________________________________|

您想解决问题吗? 我会建议你: -

  • 丢弃代码,重新设计应用程序。在您的应用程序中使用cyclic dependency是一个很大的循环漏洞,显示设计非常糟糕。