From 985cd425438183dc5194a0baeb3c0fb38eaf4e8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E5=AE=89=E6=B0=91?= <3233449392@qq.com> Date: Thu, 17 Oct 2024 16:27:35 +0800 Subject: [PATCH] 2 --- CalculatorAPP.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/CalculatorAPP.java b/CalculatorAPP.java index edf347c..8e0f925 100644 --- a/CalculatorAPP.java +++ b/CalculatorAPP.java @@ -1,4 +1,4 @@ -package Calculator; +package Calculator.jzzh; import javafx.application.Application; import javafx.geometry.Insets; @@ -52,9 +52,15 @@ public class CalculatorAPP extends Application { // 为按钮添加事件监听器 convertButton.setOnAction(e -> { try { - String num = numberField.getText(); - int fromBase = Integer.parseInt(fromBaseField.getText()); - int toBase = Integer.parseInt(toBaseField.getText()); + String num = numberField.getText().trim(); + int fromBase = Integer.parseInt(fromBaseField.getText().trim()); + int toBase = Integer.parseInt(toBaseField.getText().trim()); + + // 验证输入的有效性 + if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) { + resultLabel.setText("进制范围应在2到16之间。"); + return; + } // 调用转换方法 String converted = convertBase(num, fromBase, toBase); @@ -101,4 +107,4 @@ public class CalculatorAPP extends Application { public static void main(String[] args) { launch(args); } -} \ No newline at end of file +}