我是Java的新手...我已经有了构建科学计算器的想法,在我实现了一些actionListeners
后,我遇到了以下错误。
线程“main”java.lang.StackOverflowError中的异常 在java.awt.Insets。(Insets.java:103) at sun.awt.windows.WToolkit.getScreenInsets(Native Method) at sun.awt.windows.WToolkit.getScreenInsets(WToolkit.java:567) 在java.awt.Window.init(Window.java:498) 在java.awt.Window。(Window.java:536) 在java.awt.Frame。(Frame.java:420) 在javax.swing.JFrame。(JFrame.java:224) 在GUI。(GUI.java:29) at actionListeners。(actionListeners.java:9) 在GUI。(GUI.java:9) at actionListeners。(actionListeners.java:9) 在GUI。(GUI.java:9) at actionListeners。(actionListeners.java:9) 在GUI。(GUI.java:9) at actionListeners。(actionListeners.java:9) 在GUI。(GUI.java:9) at actionListeners。(actionListeners.java:9)
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;
}
}
}
答案 0 :(得分:0)
您尚未提供完整的代码,但您似乎在GUI
和actionListeners
类之间存在循环依赖关系。每个都需要另一个实例化,从而产生如图所示的堆栈跟踪。只需在actionListeners
中创建GUI
所需的实例,而不是相反。
建议:
答案 1 :(得分:0)
首先你的java版本是什么,你在switch语句中使用字符串。它是在java 7中实现的,在你的代码addActionListener(actionListeners)中你必须传递ActionListener对象的对象。检查那两个..