main
陈志嘉 1 month ago
parent 5b2bc1aa01
commit d16335da8e

@ -7,7 +7,7 @@ public class Calculator extends Application {
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Calculator.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("CalculatorView.fxml"));
Scene scene = new Scene(loader.load()); Scene scene = new Scene(loader.load());
primaryStage.setTitle("简易计算器"); primaryStage.setTitle("简易计算器");
primaryStage.setScene(scene); primaryStage.setScene(scene);

@ -1,71 +1,122 @@
import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
public class CalculatorController { public class CalculatorController {
@FXML @FXML
private TextField inputField; private TextField inputField;
@FXML @FXML
private TextField inputField1; private TextField inputField1; // 用于显示计算记录
@FXML private double firstNumber = 0;
private TextField inputField2; private String operator = "";
private boolean isOperationClicked = false;
@FXML @FXML
void handleAddAction(ActionEvent event) { 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);
} }
@FXML @FXML
void handleClearAction(ActionEvent event) { protected void handleAddAction(ActionEvent event) {
performOperation("+");
} }
@FXML @FXML
void handleClearsAction(ActionEvent event) { protected void handleSubtractAction(ActionEvent event) {
performOperation("-");
} }
@FXML @FXML
void handleDivideAction(ActionEvent event) { protected void handleMultiplyAction(ActionEvent event) {
performOperation("*");
} }
@FXML @FXML
void handleEqualAction(ActionEvent event) { protected void handleDivideAction(ActionEvent event) {
performOperation("/");
} }
@FXML @FXML
void handleModulusAction(ActionEvent event) { protected void handleModulusAction(ActionEvent event) {
performOperation("%");
}
private void performOperation(String op) {
firstNumber = Double.parseDouble(inputField.getText());
operator = op;
isOperationClicked = true;
} }
@FXML @FXML
void handleMultiplyAction(ActionEvent event) { 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);
}
private void recordOperation(double a, double b, double result) {
String operation = a + " " + operator + " " + b + " = " + result;
inputField1.setText(inputField1.getText() + "\n" + operation);
} }
@FXML @FXML
void handleNumberAction(ActionEvent event) { protected void handleClearAction(ActionEvent event) {
inputField.clear();
firstNumber = 0;
operator = "";
isOperationClicked = false;
} }
@FXML @FXML
void handlePointAction(ActionEvent event) { protected void handleClearsAction(ActionEvent event) {
inputField1.clear();
} }
@FXML @FXML
void handleRemoveAction(ActionEvent event) { protected void handlePointAction(ActionEvent event) {
if (!inputField.getText().contains(".")) {
inputField.setText(inputField.getText() + ".");
}
} }
@FXML @FXML
void handleSubtractAction(ActionEvent event) { protected void handleRemoveAction(ActionEvent event) {
inputField1.clear();
} }
} }

@ -1,2 +1,4 @@
# Calculator # Calculator 计算器简单运算
- `要求:`
- 实现计算器加减乘除取余等的实现

Loading…
Cancel
Save