Merge pull request '计算器' (#1) from wqf01 into main

main
fdzcxy212206209 1 month ago
commit aa518fd1b7

@ -0,0 +1,28 @@
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
public class Calculator extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws IOException {
//找到FXML文件
URL url = getClass().getResource("CalculatorView.fxml");
//用FXML创建树得到根节点
Parent root = FXMLLoader.load(url);
Scene scene = new Scene(root,282,330);
primaryStage.setTitle("计算器");
primaryStage.setScene(scene);
primaryStage.show();
}
}

@ -0,0 +1,46 @@
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class CalculatorController {
@FXML
private Label display; // 用于显示计算结果的标签
private StringBuilder currentInput = new StringBuilder(); // 存储当前输入
public void handleButtonClick(ActionEvent actionEvent) {
String buttonText = ((Button) actionEvent.getSource()).getText(); // 获取被点击按钮的文本
if (buttonText.equals("C")) {
currentInput.setLength(0); // 清空输入
display.setText("0");
} else if (buttonText.equals("=")) {
calculateResult(); // 计算结果
} else {
if (currentInput.length() < 10) { // 限制输入长度
currentInput.append(buttonText); // 更新输入
display.setText(currentInput.toString()); // 更新显示
}
}
}
private void calculateResult() {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript"); // 使用JavaScript引擎进行计算
try {
Object result = engine.eval(currentInput.toString()); // 计算结果
display.setText(result.toString()); // 更新显示
currentInput.setLength(0); // 清空当前输入
currentInput.append(result.toString()); // 将结果设置为下一个计算的输入
} catch (ScriptException e) {
display.setText("Error"); // 处理错误
currentInput.setLength(0); // 清空输入
}
}
}

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.GridPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CalculatorController">
<center>
<GridPane prefHeight="300.0" prefWidth="300.0" BorderPane.alignment="CENTER">
<Label fx:id="display" text="0" style="-fx-font-size: 24; -fx-padding: 10;" GridPane.rowIndex="0" GridPane.columnSpan="4"/>
<Button text="7" onAction="#handleButtonClick" GridPane.rowIndex="1" GridPane.columnIndex="0" minWidth="70" minHeight="70"/>
<Button text="8" onAction="#handleButtonClick" GridPane.rowIndex="1" GridPane.columnIndex="1" minWidth="70" minHeight="70"/>
<Button text="9" onAction="#handleButtonClick" GridPane.rowIndex="1" GridPane.columnIndex="2" minWidth="70" minHeight="70"/>
<Button text="/" onAction="#handleButtonClick" GridPane.rowIndex="1" GridPane.columnIndex="3" minWidth="70" minHeight="70"/>
<Button text="4" onAction="#handleButtonClick" GridPane.rowIndex="2" GridPane.columnIndex="0" minWidth="70" minHeight="70"/>
<Button text="5" onAction="#handleButtonClick" GridPane.rowIndex="2" GridPane.columnIndex="1" minWidth="70" minHeight="70"/>
<Button text="6" onAction="#handleButtonClick" GridPane.rowIndex="2" GridPane.columnIndex="2" minWidth="70" minHeight="70"/>
<Button text="*" onAction="#handleButtonClick" GridPane.rowIndex="2" GridPane.columnIndex="3" minWidth="70" minHeight="70"/>
<Button text="1" onAction="#handleButtonClick" GridPane.rowIndex="3" GridPane.columnIndex="0" minWidth="70" minHeight="70"/>
<Button text="2" onAction="#handleButtonClick" GridPane.rowIndex="3" GridPane.columnIndex="1" minWidth="70" minHeight="70"/>
<Button text="3" onAction="#handleButtonClick" GridPane.rowIndex="3" GridPane.columnIndex="2" minWidth="70" minHeight="70"/>
<Button text="-" onAction="#handleButtonClick" GridPane.rowIndex="3" GridPane.columnIndex="3" minWidth="70" minHeight="70"/>
<Button text="0" onAction="#handleButtonClick" GridPane.rowIndex="4" GridPane.columnIndex="0" minWidth="70" minHeight="70"/>
<Button text="=" onAction="#handleButtonClick" GridPane.rowIndex="4" GridPane.columnIndex="1" minWidth="70" minHeight="70"/>
<Button text="C" onAction="#handleButtonClick" GridPane.rowIndex="4" GridPane.columnIndex="2" minWidth="70" minHeight="70"/>
<Button text="+" onAction="#handleButtonClick" GridPane.rowIndex="4" GridPane.columnIndex="3" minWidth="70" minHeight="70"/>
</GridPane>
</center>
</BorderPane>
Loading…
Cancel
Save