Compare commits
No commits in common. 'master' and 'main' have entirely different histories.
@ -1,83 +0,0 @@
|
||||
package hello.demo;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
public class Calculator extends Application {
|
||||
|
||||
private TextField display;
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
primaryStage.setTitle("计算器");
|
||||
|
||||
display = new TextField();
|
||||
display.setEditable(false);
|
||||
|
||||
GridPane grid = new GridPane();
|
||||
grid.setPadding(new Insets(10));
|
||||
grid.setVgap(5);
|
||||
grid.setHgap(5);
|
||||
|
||||
// 按钮定义,包括取余运算 "%"
|
||||
String[] buttons = {
|
||||
"7", "8", "9", "/",
|
||||
"4", "5", "6", "*",
|
||||
"1", "2", "3", "-",
|
||||
"0", "C", "=", "+",
|
||||
"%"
|
||||
};
|
||||
|
||||
int index = 0;
|
||||
for (int row = 0; row < 5; row++) { // 修改行数以添加新按钮
|
||||
for (int col = 0; col < 4; col++) {
|
||||
if (index >= buttons.length) break; // 防止数组越界
|
||||
Button btn = new Button(buttons[index]);
|
||||
btn.setMinSize(50, 50);
|
||||
btn.setOnAction(e -> handleButtonClick(btn.getText()));
|
||||
grid.add(btn, col, row);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
grid.add(display, 0, 5, 4, 1); // 显示区域在最后一行
|
||||
|
||||
Scene scene = new Scene(grid, 250, 350);
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
private void handleButtonClick(String text) {
|
||||
if ("C".equals(text)) {
|
||||
display.clear();
|
||||
} else if ("=".equals(text)) {
|
||||
calculateResult();
|
||||
} else {
|
||||
display.appendText(text);
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateResult() {
|
||||
try {
|
||||
String input = display.getText();
|
||||
// 使用脚本引擎执行表达式
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngine engine = manager.getEngineByName("JavaScript");
|
||||
Object result = engine.eval(input);
|
||||
display.setText(result.toString());
|
||||
} catch (Exception e) {
|
||||
display.setText("错误");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
@ -1,83 +0,0 @@
|
||||
package hello.demo;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
public class Calculator extends Application {
|
||||
|
||||
private TextField display;
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
primaryStage.setTitle("计算器");
|
||||
|
||||
display = new TextField();
|
||||
display.setEditable(false);
|
||||
|
||||
GridPane grid = new GridPane();
|
||||
grid.setPadding(new Insets(10));
|
||||
grid.setVgap(5);
|
||||
grid.setHgap(5);
|
||||
|
||||
// 按钮定义,包括取余运算 "%"
|
||||
String[] buttons = {
|
||||
"7", "8", "9", "/",
|
||||
"4", "5", "6", "*",
|
||||
"1", "2", "3", "-",
|
||||
"0", "C", "=", "+",
|
||||
"%"
|
||||
};
|
||||
|
||||
int index = 0;
|
||||
for (int row = 0; row < 5; row++) { // 修改行数以添加新按钮
|
||||
for (int col = 0; col < 4; col++) {
|
||||
if (index >= buttons.length) break; // 防止数组越界
|
||||
Button btn = new Button(buttons[index]);
|
||||
btn.setMinSize(50, 50);
|
||||
btn.setOnAction(e -> handleButtonClick(btn.getText()));
|
||||
grid.add(btn, col, row);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
grid.add(display, 0, 5, 4, 1); // 显示区域在最后一行
|
||||
|
||||
Scene scene = new Scene(grid, 250, 350);
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
private void handleButtonClick(String text) {
|
||||
if ("C".equals(text)) {
|
||||
display.clear();
|
||||
} else if ("=".equals(text)) {
|
||||
calculateResult();
|
||||
} else {
|
||||
display.appendText(text);
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateResult() {
|
||||
try {
|
||||
String input = display.getText();
|
||||
// 使用脚本引擎执行表达式
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngine engine = manager.getEngineByName("JavaScript");
|
||||
Object result = engine.eval(input);
|
||||
display.setText(result.toString());
|
||||
} catch (Exception e) {
|
||||
display.setText("错误");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue