用 Javafx编写一个能够进行四则运算的计算器放在一个 class 内计算器eclipse
时间: 2024-09-14 17:09:46 浏览: 40
jisuanqi.zip_eclipse 计算器
5星 · 资源好评率100%
在JavaFX中编写一个简单的四则运算计算器,可以创建一个`Calculator`类,该类包含各种静态方法来执行加、减、乘、除操作。下面是一个基本的实现示例:
```java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Calculator extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("简单计算器");
Label display = new Label();
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
// 数字按钮
for (int i = 0; i <= 9; i++) {
Button button = new Button(Integer.toString(i));
button.setOnAction(e -> {
String input = display.getText() + button.getText();
display.setText(input);
});
grid.add(button, i % 4, i / 4);
}
// 运算符按钮
Button addButton = new Button("+");
addButton.setOnAction(e -> add(display.getText()));
grid.add(addButton, 3, 3);
Button subtractButton = new Button("-");
subtractButton.setOnAction(e -> subtract(display.getText()));
grid.add(subtractButton, 0, 3);
Button multiplyButton = new Button("*");
multiplyButton.setOnAction(e -> multiply(display.getText()));
grid.add(multiplyButton, 1, 3);
Button divideButton = new Button("/");
divideButton.setOnAction(e -> divide(display.getText()));
grid.add(divideButton, 2, 3);
// 清零按钮
Button clearButton = new Button("C");
clearButton.setOnAction(e -> display.setText(""));
grid.add(clearButton, 0, 0);
// 等于按钮
Button equalsButton = new Button "=";
equalsButton.setOnAction(e -> performCalculation(display.getText(), "=", display));
grid.add(equalsButton, 3, 0);
HBox buttonsRow = new HBox(grid);
VBox root = new VBox(10, display, buttonsRow);
Scene scene = new Scene(root, 300, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private void add(String currentText) {
double result = Double.parseDouble(currentText) + 1;
display.setText(result + "");
}
private void subtract(String currentText) {
double result = Double.parseDouble(currentText) - 1;
display.setText(result + "");
}
private void multiply(String currentText) {
double result = Double.parseDouble(currentText) * 2;
display.setText(result + "");
}
private void divide(String currentText) {
if (!currentText.isEmpty()) {
try {
double result = Double.parseDouble(currentText) / 2;
display.setText(result + "");
} catch (NumberFormatException e) {
display.setText("错误,请输入数字");
}
}
}
private void performCalculation(String currentText, String operator, Label display) {
if (currentText.isEmpty()) return;
try {
double num1 = Double.parseDouble(currentText);
double num2 = calculateNextNumber(display.getText());
switch (operator) {
case "+":
display.setText(num1 + num2 + "");
break;
case "-":
display.setText(num1 - num2 + "");
break;
case "*":
display.setText(num1 * num2 + "");
break;
case "/":
display.setText(num1 / num2 + "");
break;
default:
throw new IllegalArgumentException("未知的运算符");
}
} catch (NumberFormatException e) {
display.setText("错误,请输入数字");
}
}
private double calculateNextNumber(String text) {
int index = text.indexOf(".");
if (index == -1) {
return Double.parseDouble(text);
} else {
return Double.parseDouble(text.substring(0, index + 1));
}
}
}
```
这个例子展示了如何在一个JavaFX窗口上创建一个基础的计算器应用,包括数字按钮、运算符按钮以及清零和等于按钮。当用户点击等号按钮时,会执行相应的计算。
阅读全文