-1

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

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

1 回答 1

6

ExplorerGame中,您已声明:-

ExplorerPanel gui = new ExplorerPanel();

然后,在ExplorerPanel中:-

ExplorerEvent prog = new ExplorerEvent(this);

然后再一次,在ExplorerEvent中:-

ExplorerGame game = new ExplorerGame();

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

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

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

  • 扔掉代码,重新设计你的应用程序。在cyclic dependency你的应用程序中有一个很大的漏洞,显示出一个非常糟糕的设计。
于 2012-10-23T16:32:14.563 回答