当前位置: 首页 > 知识库问答 >
问题:

线程“main”java.lang.StackOverflowError actionListeners中出现异常

阴培
2023-03-14

线程“main”java.lang.StackOverflowError在java.awt.insets处出现异常。(insets.java:103)在sun.awt.windows.wtoolkit.getScreenInsets(本机方法)在sun.awt.windows.wtoolkit.getScreenInsets(wtoolkit.getScreenInsets(wtoolkit.java:567)在java.awt.window.init(window.java:498)在java.awt.window.init(window.java:536)在java.awt.frame

and this error keeps showing alot . What could be the problem ? 

这是密码

public GUI() {
    super("Calculator");
    setLayout(new GridLayout(5,2));

    result = new JTextArea();
    result.setEditable(false);
    add(result);
    divide.addActionListener(actionListeners);
    add(divide);
    multiply.addActionListener(actionListeners);
    add(multiply);
    substract.addActionListener(actionListeners);
    add(substract);
    sum.addActionListener(actionListeners);
    add(sum);
    for(int i=0;i<=numberButtons.length-1;i++)
 {
     numberButtons[i]= new JButton(Integer.toString(i));}

    add(numberButtons[7]);
    add(numberButtons[8]);
    add(numberButtons[9]);
    add(C);
    add(CE);
    add(numberButtons[4]);
    add(numberButtons[5]);
    add(numberButtons[6]);
    add(sqrt);
    add(cubic);
    add(numberButtons[1]);
    add(numberButtons[2]);
    add(numberButtons[3]);
    add(percentage);
    add(divideByOne);
    add(numberButtons[0]);
    add(point);
    add(square);
    add(OK);

}
public void setTextResult(String a) {
    result.setText(a);
  }
}


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.naming.spi.DirStateFactory.Result;

public class actionListeners implements ActionListener {

Functions fn = new Functions();
GUI go = new GUI();


public void actionPerformed(ActionEvent e) {
    String x = e.getActionCommand();
    switch (x) {
    case "/":
        fn.divide(fn.getA(), fn.getB());
        go.setTextResult(Double.toString(fn.getResult()));
        break;
    case "*":
        fn.multiply(fn.getA(), fn.getB());
        break;
    case "-":
        fn.substract(fn.getA(), fn.getB());
        break;
    case "+":
        fn.sum(fn.getA(), fn.getB());
        break;
    case "+-":
        fn.divide(fn.getA(), fn.getB());
        break;
    case "C":
        go.setTextResult(" ");
        break;
    case "CE":
        go.setTextResult(" ");
        fn.setResult(0);
        break;
    case "sqrt":
        fn.sqrt(fn.getA());
        break;
    case "OK":
        go.setTextResult(Double.toString(fn.getResult()));
        break;
    case "SQRT":
        break;
    case "%":
        break;
    case "x^2":
        break;
    case "x^3":
        break;
    case "1/x":
        break;
    default:
        go.setTextResult("Eroare");
        break;
    }
}
}

共有1个答案

华俊弼
2023-03-14

您没有提供完整的代码,但GUIActionListeners类之间似乎存在循环依赖关系。每一个都需要实例化另一个,从而产生如图所示的stacktrace。只需在GUI中创建所需的ActionListeners实例,而不是相反。

建议:

  • 考虑在按钮之间使用共享功能的操作
 类似资料: