是否有人可以解釋爲什麼我收到錯誤信息......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引用參考變量的實例,是他們的任何錯誤他們?
所以你建議我刪除引用變量之一,無論是控制器或motionObj? – RamanSB
你必須在MotionEvent中刪除它不需要其他 – Madhan
請[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)如果它幫助解決了這個問題。我想這很可能是探針的原因 – Madhan