2015-07-02 31 views
1

是否有人可以解釋爲什麼我收到錯誤信息......StackOverflow上的GUI類

我有兩個班,一個在其中創建一個GUI和第二類來處理事件。源代碼如下所示...

CLASS 1

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

public class Motion extends JFrame { 

MotionEvent controller = new MotionEvent(); 

//row 0 
JPanel row0 = new JPanel(); 

//row 1 
JPanel row1 = new JPanel(); 
JButton up = new JButton("Up"); 

//row 2 
JPanel row2 = new JPanel(); 
JButton left = new JButton("Left"); 
JButton right = new JButton("Right"); 
JCheckBox compassFormat = new JCheckBox("compassFormat", false); 

//row 3 
JPanel row3 = new JPanel(); 
JButton down = new JButton("down"); 

Motion(){ 
    super("Motion Detector"); 
    setSize(500,325); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    GridLayout layoutMaster = new GridLayout(5,1,10,10); 
    setLayout(layoutMaster); 

    add(row0); 

    FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER); 
    row1.setLayout(layout1); 
    up.addActionListener(controller); 
    row1.add(up); 
    add(row1); 

    GridLayout layout2 = new GridLayout(1, 3, 10, 10); 
    row2.setLayout(layout2); 
    left.addActionListener(controller); 
    //compassFormat.addItemListener(controller); 
    right.addActionListener(controller); 
    row2.add(left); 
    row2.add(compassFormat); 
    row2.add(right); 
    add(row2); 

    FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER); 
    row3.setLayout(layout3); 
    row3.add(down); 
    add(row3); 

    setVisible(true); 

} 

public static void main(String[] args){ 
    Motion createGui = new Motion(); 
    } 
} 

CLASS 2

import java.awt.event.*; 
    import javax.swing.*; 
public class MotionEvent implements ActionListener/*, ItemListener */{ 

Motion motionObj = new Motion(); 

public void actionPerformed(ActionEvent event){ 
    Object objSource = event.getSource(); 
    if(objSource == motionObj.up){ 
     JOptionPane.showMessageDialog(null, "You have moved up"); 
    } 
    else if(objSource == motionObj.down){ 
     JOptionPane.showMessageDialog(null, "You have moved down"); 
    } 
    else if(objSource == motionObj.left){ 
     JOptionPane.showMessageDialog(null, "You have moved left"); 
    } 
    else if(objSource == motionObj.right){ 
     JOptionPane.showMessageDialog(null, "You have moved right", "Navigator", JOptionPane.INFORMATION_MESSAGE); 
    } 
} 

//public void itemStateChanged(ItemEvent event){ 


}// 

接收到的錯誤信息是:

Exception in thread "main" java.lang.StackOverflowError 
at sun.awt.Win32GraphicsConfig.getBounds(Native Method) 
at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source) 
at java.awt.Window.init(Unknown Source) 
at java.awt.Window.<init>(Unknown Source) 
at java.awt.Frame.<init>(Unknown Source) 
at javax.swing.JFrame.<init>(Unknown Source) 
at Motion.<init>(Motion.java:26) 
at MotionEvent.<init>(MotionEvent.java:5) 
at Motion.<init>(Motion.java:6) 

Motion.java:6 & MotionEvent.java:5引用參考變量的實例,是他們的任何錯誤他們?

回答

1

有一個無限循環running.Means Motion被無限創造

每當創建了Motion對象爲MotionEvent對象還創建

public class Motion extends JFrame { 

MotionEvent controller = new MotionEvent(); 

,每當創建一個用於MotionEvent對象創建對象Motion

public class MotionEvent implements ActionListener/*, ItemListener */{ 

    Motion motionObj = new Motion();//which will initiate endless call for this.Remove this 

反過來導致這兩個對象的無盡創建,這明顯導致了stackoverflow錯誤。 刪除

+0

所以你建議我刪除引用變量之一,無論是控制器或motionObj? – RamanSB

+0

你必須在MotionEvent中刪除它不需要其他 – Madhan

+0

請[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)如果它幫助解決了這個問題。我想這很可能是探針的原因 – Madhan

1

,這是一個遞歸調用Motion對象使用您的MotionEvent類以下

import java.awt.event.*; 
    import javax.swing.*; 
public class MotionEvent implements ActionListener/*, ItemListener */{ 
public void actionPerformed(ActionEvent event){ 
    Object objSource = event.getActionCommand(); 
    if(objSource.equals("Up")){ 
     JOptionPane.showMessageDialog(null, "You have moved up"); 
    } 
    else if(objSource .equals("Down")){ 
     JOptionPane.showMessageDialog(null, "You have moved down"); 
    } 
    else if(objSource.equals("Left")){ 
     JOptionPane.showMessageDialog(null, "You have moved left"); 
    } 
    else if(objSource.equals("Right")){ 
     JOptionPane.showMessageDialog(null, "You have moved right", "Navigator", JOptionPane.INFORMATION_MESSAGE); 
    } 
} 

//public void itemStateChanged(ItemEvent event){ 


}// 
+0

是的,我做到了。 由於某種原因,當我點擊一個按鈕我的GUI時,它只是打開一個完全相同的GUI彈出。我不認爲我的事件處理代碼正常工作。 它是這樣設計的,當一個按鈕被按下時,會出現一個消息框,說明哪個方向被點擊,因此用戶在指定的方向上移動。出於某種原因,單擊(J)按鈕時會出現GUI的新彈出窗口。 – RamanSB

+0

不客氣....... –