我正在为计算器编写程序,我已将其添加到我的主Java文件中。
CalculatorEngine calcEngine = new CalculatorEngine();
这样对我的类文件如此:
public class CalculatorEngine implements ActionListener {//code here ;}
谁能告诉我我做错了什么?
这是错误消息:"构造函数CalculatorEngine()未定义"但我认为你是如何定义它的?
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class CalculatorEngine implements ActionListener {
Calculator parent; //a reference to Calculator window
char selectedAction = ' '; // +, -, /, or *
double currentResult =0;
// Constructor stores the reference to the Calculator
// window in the member variable parent
CalculatorEngine(Calculator parent){
this.parent = parent;
}
public void actionPerformed(ActionEvent e){
// Get the source of this action
JButton clickedButton = (JButton) e.getSource();
String dispFieldText=parent.displayField.getText();
double displayValue=0;
//Get the number from the text field // if it’s not empty
if (!"".equals(dispFieldText)){
displayValue=Double.parseDouble(dispFieldText);
}
Object src = e.getSource();
//For each action button memorise selected
//action +, -, /, or *, store the current value
//in the currentResult, and clean up the display
//field for entering the next number
if (src == parent.buttonPlus){ selectedAction = '+';
currentResult=displayValue; parent.displayField.setText("" );
} else if (src == parent.buttonMinus){ selectedAction = '-';
currentResult=displayValue; parent.displayField.setText("");
}else if (src == parent.buttonDivide){ selectedAction = '/';
currentResult=displayValue; parent.displayField.setText("");
} else if (src == parent.buttonMultiply){ selectedAction = '*';
currentResult=displayValue; parent.displayField.setText("" );
} else if (src == parent.buttonEqual){
//Perform the calculations based on selectedAction
//update the value of the variable currentResult
//and display the result
if (selectedAction=='+'){
currentResult +=displayValue;
//Convert the result to String by concatenating
//to an empty string and display it
parent.displayField.setText(""+currentResult );
}else if (selectedAction=='-'){ currentResult -=displayValue;
parent.displayField.setText(""+currentResult); }
else if (selectedAction=='/'){
currentResult /=displayValue;
parent.displayField.setText(""+currentResult); }
else if (selectedAction=='*'){
currentResult*=displayValue;
parent.displayField.setText(""+currentResult);
}
} else{
// For all numeric buttons append the button's
// label to the text field
String clickedButtonLabel= clickedButton.getText();
parent.displayField.setText(dispFieldText + clickedButtonLabel);
}}}
这是班级。
答案 0 :(得分:3)
您的代码包含一个具有包级别(默认)权限的构造函数,
CalculatorEngine(Calculator parent){
this.parent = parent;
}
因此,您没有获得默认构造函数,并且您无法调用构造函数,该构造函数在任何地方都使用Calculator
但是相同的包(或子类)。添加一个空的公共构造函数(或删除现有的构造函数,你将得到默认的构造函数)。
public CalculatorEngine(){
super();
}
答案 1 :(得分:0)
您正尝试将任何内容传递给CalculatorEngine
唯一的构造函数。但是,该构造函数采用一个参数Calculator
。
将计算器对象传递给构造函数。
CalculatorEngine calcEngine = new CalculatorEngine(aCalculatorObject);
答案 2 :(得分:0)
这是构造函数:
CalculatorEngine(Calculator parent)
所以你必须在建造时提供一个计算器。您无法调用new CalculatorEngine()
,因为没有无参数构造函数。
请注意,在没有任何其他构造函数的情况下,无需定义就可以使用无参数构造函数。见https://softwareengineering.stackexchange.com/questions/257938/why-is-no-default-constructor-generated-if-you-define-an-explicit-constructor