From b3621b4905c1e718536879bc2bfcfb45aff67217 Mon Sep 17 00:00:00 2001 From: LIN <2740625745@qq.com> Date: Thu, 10 Oct 2024 18:01:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E5=88=B6=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++++ .idea/group.iml | 9 +++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ BaseConverterApp.java | 93 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 130 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/group.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 BaseConverterApp.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/group.iml b/.idea/group.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/group.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..454cc2f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BaseConverterApp.java b/BaseConverterApp.java new file mode 100644 index 0000000..09d27c2 --- /dev/null +++ b/BaseConverterApp.java @@ -0,0 +1,93 @@ +import javafx.application.Application; +import javafx.geometry.Insets; +import javafx.scene.Scene; +import javafx.scene.control.*; +import javafx.scene.layout.GridPane; +import javafx.stage.Stage; + +public class BaseConverterApp extends Application { + + private TextField inputField; + private ComboBox fromBaseBox; + private ComboBox toBaseBox; + private TextField resultField; + + public static void main(String[] args) { + launch(args); + } + + @Override + public void start(Stage primaryStage) { + primaryStage.setTitle("Base Converter"); + + // 创建网格布局 + GridPane grid = new GridPane(); + grid.setPadding(new Insets(10, 10, 10, 10)); + grid.setVgap(10); + grid.setHgap(10); + + // 创建输入框 + Label inputLabel = new Label("Input Number:"); + grid.add(inputLabel, 0, 0); + inputField = new TextField(); + grid.add(inputField, 1, 0); + + // 创建“From Base”下拉菜单 + Label fromBaseLabel = new Label("From Base:"); + grid.add(fromBaseLabel, 0, 1); + fromBaseBox = new ComboBox<>(); + for (int i = 2; i <= 16; i++) { + fromBaseBox.getItems().add(i); + } + fromBaseBox.setValue(2); // 默认二进制 + grid.add(fromBaseBox, 1, 1); + + // 创建“To Base”下拉菜单 + Label toBaseLabel = new Label("To Base:"); + grid.add(toBaseLabel, 0, 2); + toBaseBox = new ComboBox<>(); + for (int i = 2; i <= 16; i++) { + toBaseBox.getItems().add(i); + } + toBaseBox.setValue(2); // 默认二进制 + grid.add(toBaseBox, 1, 2); + + // 创建结果框 + Label resultLabel = new Label("Result:"); + grid.add(resultLabel, 0, 3); + resultField = new TextField(); + resultField.setEditable(false); // 结果框为只读 + grid.add(resultField, 1, 3); + + // 创建转换按钮 + Button convertButton = new Button("Convert"); + convertButton.setOnAction(e -> convert()); + grid.add(convertButton, 0, 4, 2, 1); + + // 设置场景并显示 + Scene scene = new Scene(grid, 300, 250); + primaryStage.setScene(scene); + primaryStage.show(); + } + + private void convert() { + try { + // 获取输入的数字和选择的进制 + String inputNumber = inputField.getText(); + int fromBase = fromBaseBox.getValue(); + int toBase = toBaseBox.getValue(); + + // 将输入的数字从起始进制转换为十进制 + int decimalValue = Integer.parseInt(inputNumber, fromBase); + + // 将十进制数字转换为目标进制 + String result = Integer.toString(decimalValue, toBase).toUpperCase(); + + // 显示转换结果 + resultField.setText(result); + } catch (NumberFormatException ex) { + // 如果输入无效,显示错误信息 + resultField.setText("Invalid Input"); + } + } +}