parent
cb4310a83a
commit
407dcadfeb
@ -0,0 +1,26 @@
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Calculator extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws IOException {
|
||||
// 加载FXML文件
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/CalculatorView.fxml"));
|
||||
Parent root = fxmlLoader.load();
|
||||
|
||||
Scene scene = new Scene(root, 600, 500);
|
||||
primaryStage.setTitle("计算器");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextArea;
|
||||
|
||||
public class CalculatorControl {
|
||||
|
||||
@FXML
|
||||
private TextArea display;
|
||||
|
||||
private double firstNumber;
|
||||
private double secondNumber;
|
||||
private String operation;
|
||||
private boolean awaitingSecondNumber;
|
||||
|
||||
@FXML
|
||||
void onClearButtonClicked(ActionEvent event) {
|
||||
display.clear();
|
||||
firstNumber = 0;
|
||||
secondNumber = 0;
|
||||
operation = null;
|
||||
awaitingSecondNumber = false;
|
||||
}
|
||||
|
||||
@FXML
|
||||
void onClearEntryButtonClicked(ActionEvent event) {
|
||||
display.clear();
|
||||
}
|
||||
|
||||
@FXML
|
||||
void onDeleteButtonClicked(ActionEvent event) {
|
||||
String currentText = display.getText();
|
||||
if (currentText != null && !currentText.isEmpty()) {
|
||||
display.setText(currentText.substring(0, currentText.length() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
void onEqualsButtonClicked(ActionEvent event) {
|
||||
if (awaitingSecondNumber) {
|
||||
String displayText = display.getText();
|
||||
String[] parts = displayText.split(" ");
|
||||
try {
|
||||
secondNumber = Double.parseDouble(parts[parts.length - 1]);
|
||||
switch (operation) {
|
||||
case "+":
|
||||
display.setText(firstNumber + " + " + secondNumber + " = " + (firstNumber + secondNumber));
|
||||
break;
|
||||
case "-":
|
||||
display.setText(firstNumber + " - " + secondNumber + " = " + (firstNumber - secondNumber));
|
||||
break;
|
||||
case "*":
|
||||
display.setText(firstNumber + " * " + secondNumber + " = " + (firstNumber * secondNumber));
|
||||
break;
|
||||
case "/":
|
||||
if (secondNumber == 0) {
|
||||
display.setText("Error");
|
||||
} else {
|
||||
display.setText(firstNumber + " / " + secondNumber + " = " + (firstNumber / secondNumber));
|
||||
}
|
||||
break;
|
||||
case "%":
|
||||
display.setText(firstNumber + " % " + secondNumber + " = " + (firstNumber % secondNumber));
|
||||
break;
|
||||
}
|
||||
awaitingSecondNumber = false;
|
||||
} catch (NumberFormatException e) {
|
||||
display.setText("Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
void onNumberButtonClicked(ActionEvent event) {
|
||||
Button button = (Button) event.getSource();
|
||||
String buttonText = button.getText();
|
||||
display.appendText(buttonText);
|
||||
}
|
||||
|
||||
@FXML
|
||||
void onOperationButtonClicked(ActionEvent event) {
|
||||
Button button = (Button) event.getSource();
|
||||
String buttonText = button.getText();
|
||||
if (!awaitingSecondNumber) {
|
||||
String displayText = display.getText();
|
||||
String[] parts = displayText.split(" ");
|
||||
try {
|
||||
firstNumber = Double.parseDouble(parts[parts.length - 1]);
|
||||
operation = buttonText;
|
||||
awaitingSecondNumber = true;
|
||||
display.appendText(" " + buttonText + " ");
|
||||
} catch (NumberFormatException e) {
|
||||
display.setText("Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue