有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

GUI类中的java堆栈溢出

有人能解释一下我为什么会收到错误信息吗

我有两个类,一个是创建GUI的类,另一个是处理事件的类。源代码如下所示

一班

        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)

动议。爪哇:6&;运动事件。java:5参考引用变量的实例化,它们有什么问题吗


共 (2) 个答案

  1. # 1 楼答案

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

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

  2. # 2 楼答案

    这是对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){
    
    
    }//