2012-10-23 121 views
-1

當我運行我製作的這個短程序時,我不斷收到堆棧溢出錯誤!請幫忙!現在,它應該做的就是接受用戶輸入並打印它們的位置(在X和Y座標中)。我不確定是什麼堆棧溢出錯誤或如何解決它。堆棧Oveflow錯誤

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

*請在未來發布之前格式化您的代碼。由於缺乏縮進,原來的問題很難看懂。 –

+0

stackoverflow錯誤通常意味着你做了一個遞歸調用,應該是函數調用自己或類似的。使用調試器並跟蹤問題。 – blackuprise

+0

堆棧跟蹤將顯示錯誤的位置。 –

回答

7

ExplorerGame,聲明瞭: -

ExplorerPanel gui = new ExplorerPanel(); 

然後,在ExplorerPanel: -

ExplorerEvent prog = new ExplorerEvent(this); 

,然後再在ExplorerEvent: -

ExplorerGame game = new ExplorerGame(); 

這將填充堆棧以遞歸創建對象。

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

您想解決問題嗎? 我會建議你: -

  • 扔掉代碼,並重新設計你的應用程序。在你的應用程序中有cyclic dependency是一個很大的循環漏洞,顯示了非常糟糕的設計。