parent
778e269786
commit
5b2bc1aa01
@ -1,122 +1,71 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
private double firstNumber = 0;
|
@FXML
|
||||||
private String operator = "";
|
private TextField inputField2;
|
||||||
private boolean isOperationClicked = false;
|
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handleNumberAction(ActionEvent event) {
|
void handleAddAction(ActionEvent event) {
|
||||||
Button button = (Button) event.getSource();
|
|
||||||
String number = button.getText();
|
|
||||||
if (isOperationClicked) {
|
|
||||||
inputField.clear();
|
|
||||||
isOperationClicked = false;
|
|
||||||
}
|
|
||||||
inputField.setText(inputField.getText() + number);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handleAddAction(ActionEvent event) {
|
void handleClearAction(ActionEvent event) {
|
||||||
performOperation("+");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handleSubtractAction(ActionEvent event) {
|
void handleClearsAction(ActionEvent event) {
|
||||||
performOperation("-");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handleMultiplyAction(ActionEvent event) {
|
void handleDivideAction(ActionEvent event) {
|
||||||
performOperation("*");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handleDivideAction(ActionEvent event) {
|
void handleEqualAction(ActionEvent event) {
|
||||||
performOperation("/");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handleModulusAction(ActionEvent event) {
|
void handleModulusAction(ActionEvent event) {
|
||||||
performOperation("%");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void performOperation(String op) {
|
|
||||||
firstNumber = Double.parseDouble(inputField.getText());
|
|
||||||
operator = op;
|
|
||||||
isOperationClicked = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handleEqualAction(ActionEvent event) {
|
void handleMultiplyAction(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
|
||||||
protected void handleClearAction(ActionEvent event) {
|
void handleNumberAction(ActionEvent event) {
|
||||||
inputField.clear();
|
|
||||||
firstNumber = 0;
|
|
||||||
operator = "";
|
|
||||||
isOperationClicked = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handleClearsAction(ActionEvent event) {
|
void handlePointAction(ActionEvent event) {
|
||||||
inputField1.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handlePointAction(ActionEvent event) {
|
void handleRemoveAction(ActionEvent event) {
|
||||||
if (!inputField.getText().contains(".")) {
|
|
||||||
inputField.setText(inputField.getText() + ".");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
protected void handleRemoveAction(ActionEvent event) {
|
void handleSubtractAction(ActionEvent event) {
|
||||||
inputField1.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue