From f1fa110b9a95135993e8fdc3ef3ab4891a853874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=9F=8E?= <2304508674@qq.com> Date: Thu, 17 Oct 2024 16:13:36 +0800 Subject: [PATCH] 1 --- CalculatorAPP.java | 104 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 CalculatorAPP.java diff --git a/CalculatorAPP.java b/CalculatorAPP.java new file mode 100644 index 0000000..edf347c --- /dev/null +++ b/CalculatorAPP.java @@ -0,0 +1,104 @@ +package Calculator; + +import javafx.application.Application; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; +import javafx.scene.layout.GridPane; +import javafx.stage.Stage; + +public class CalculatorAPP extends Application { + + private TextField numberField; + private TextField fromBaseField; + private TextField toBaseField; + private Label resultLabel; + + @Override + public void start(Stage primaryStage) { + // 创建UI组件 + Label numberLabel = new Label("请输入要转换的数:"); + numberField = new TextField(); + + Label fromBaseLabel = new Label("请输入源进制(2-16):"); + fromBaseField = new TextField(); + + Label toBaseLabel = new Label("请输入目标进制(2-16):"); + toBaseField = new TextField(); + + Button convertButton = new Button("转换"); + resultLabel = new Label(); + + // 创建布局 + GridPane gridPane = new GridPane(); + gridPane.setAlignment(Pos.CENTER); + gridPane.setHgap(10); + gridPane.setVgap(10); + gridPane.setPadding(new Insets(20)); + + // 添加组件到布局 + gridPane.add(numberLabel, 0, 0); + gridPane.add(numberField, 1, 0); + gridPane.add(fromBaseLabel, 0, 1); + gridPane.add(fromBaseField, 1, 1); + gridPane.add(toBaseLabel, 0, 2); + gridPane.add(toBaseField, 1, 2); + gridPane.add(convertButton, 0, 3); + gridPane.add(resultLabel, 1, 3); + + // 为按钮添加事件监听器 + convertButton.setOnAction(e -> { + try { + String num = numberField.getText(); + int fromBase = Integer.parseInt(fromBaseField.getText()); + int toBase = Integer.parseInt(toBaseField.getText()); + + // 调用转换方法 + String converted = convertBase(num, fromBase, toBase); + + // 更新结果显示 + resultLabel.setText(String.format("%s (%d进制) 转换为 (%d进制) 是 %s", num, fromBase, toBase, converted)); + } catch (NumberFormatException ex) { + resultLabel.setText("输入有误,请检查输入是否正确。"); + } + }); + + // 创建场景 + Scene scene = new Scene(gridPane, 400, 300); + + // 设置舞台 + primaryStage.setTitle("进制转换器"); + primaryStage.setScene(scene); + primaryStage.show(); + } + + public static String convertBase(String num, int fromBase, int toBase) { + // 将给定的fromBase进制数转换为十进制数 + int decimalNum = Integer.parseInt(num, fromBase); + + // 定义字符集以表示大于10的进制数 + char[] digits = "0123456789ABCDEF".toCharArray(); + + // 如果目标进制是10,则直接返回decimalNum + if (toBase == 10) { + return String.valueOf(decimalNum); + } + + // 否则,从十进制转换为目标进制 + StringBuilder result = new StringBuilder(); + while (decimalNum > 0) { + int remainder = decimalNum % toBase; + result.insert(0, digits[remainder]); + decimalNum /= toBase; + } + + return result.toString(); + } + + public static void main(String[] args) { + launch(args); + } +} \ No newline at end of file