You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Calculator/CalculatorController.java

123 lines
3.2 KiB

1 month ago
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
1 month ago
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
1 month ago
public class CalculatorController {
@FXML
private TextField inputField;
@FXML
1 month ago
private TextField inputField1; // 用于显示计算记录
1 month ago
1 month ago
private double firstNumber = 0;
private String operator = "";
private boolean isOperationClicked = false;
1 month ago
@FXML
1 month ago
protected void handleNumberAction(ActionEvent event) {
Button button = (Button) event.getSource();
String number = button.getText();
if (isOperationClicked) {
inputField.clear();
isOperationClicked = false;
}
inputField.setText(inputField.getText() + number);
1 month ago
}
@FXML
1 month ago
protected void handleAddAction(ActionEvent event) {
performOperation("+");
1 month ago
}
@FXML
1 month ago
protected void handleSubtractAction(ActionEvent event) {
performOperation("-");
1 month ago
}
@FXML
1 month ago
protected void handleMultiplyAction(ActionEvent event) {
performOperation("*");
1 month ago
}
@FXML
1 month ago
protected void handleDivideAction(ActionEvent event) {
performOperation("/");
1 month ago
}
@FXML
1 month ago
protected void handleModulusAction(ActionEvent event) {
performOperation("%");
}
1 month ago
1 month ago
private void performOperation(String op) {
firstNumber = Double.parseDouble(inputField.getText());
operator = op;
isOperationClicked = true;
1 month ago
}
@FXML
1 month ago
protected void handleEqualAction(ActionEvent event) {
double secondNumber = Double.parseDouble(inputField.getText());
double result = 0;
switch (operator) {
case "+":
result = firstNumber + secondNumber;
break;
case "-":
result = firstNumber - secondNumber;
break;
case "*":
result = firstNumber * secondNumber;
break;
case "/":
if (secondNumber != 0) {
result = firstNumber / secondNumber;
} else {
inputField.setText("Error");
return;
}
break;
case "%":
result = firstNumber % secondNumber;
break;
default:
return;
}
inputField.setText(String.valueOf(result));
recordOperation(firstNumber, secondNumber, result);
}
1 month ago
1 month ago
private void recordOperation(double a, double b, double result) {
String operation = a + " " + operator + " " + b + " = " + result;
inputField1.setText(inputField1.getText() + "\n" + operation);
1 month ago
}
@FXML
1 month ago
protected void handleClearAction(ActionEvent event) {
inputField.clear();
firstNumber = 0;
operator = "";
isOperationClicked = false;
1 month ago
}
@FXML
1 month ago
protected void handleClearsAction(ActionEvent event) {
inputField1.clear();
1 month ago
}
@FXML
1 month ago
protected void handlePointAction(ActionEvent event) {
if (!inputField.getText().contains(".")) {
inputField.setText(inputField.getText() + ".");
}
1 month ago
}
@FXML
1 month ago
protected void handleRemoveAction(ActionEvent event) {
inputField1.clear();
1 month ago
}
}