StackOverflow in GUI class



有人可以解释为什么我会收到错误消息...

我有两个类,一个创建 GUI,第二个类处理事件。源代码如下所示...

第 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();
    }
}

第 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 指的是引用变量的实例化,它们有什么问题吗?

有一个无限循环在运行。意味着无限创造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

这反过来又导致这两个对象的无休止创建,这显然会导致堆栈溢出错误。删除

这是

对对象的递归调用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){

}//

相关内容

  • 没有找到相关文章

最新更新