|
|
|
|
@ -0,0 +1,97 @@
|
|
|
|
|
package jzzh;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
|
// 将任意 R 进制的数转换为十进制
|
|
|
|
|
public static int convertToDecimal(String number, int base) {
|
|
|
|
|
return Integer.parseInt(number, base);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将十进制数转换为任意目标进制
|
|
|
|
|
public static String convertFromDecimal(int decimalNumber, int targetBase) {
|
|
|
|
|
return Integer.toString(decimalNumber, targetBase).toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void start(Stage primaryStage) {
|
|
|
|
|
primaryStage.setTitle("Base Converter");
|
|
|
|
|
|
|
|
|
|
// 创建布局
|
|
|
|
|
GridPane grid = new GridPane();
|
|
|
|
|
grid.setPadding(new Insets(10, 10, 10, 10));
|
|
|
|
|
grid.setVgap(8);
|
|
|
|
|
grid.setHgap(10);
|
|
|
|
|
|
|
|
|
|
// 原始进制标签和输入框
|
|
|
|
|
Label originalBaseLabel = new Label("原始进制 (2-16):");
|
|
|
|
|
GridPane.setConstraints(originalBaseLabel, 0, 0);
|
|
|
|
|
TextField originalBaseInput = new TextField();
|
|
|
|
|
GridPane.setConstraints(originalBaseInput, 1, 0);
|
|
|
|
|
|
|
|
|
|
// 要转换的数字标签和输入框
|
|
|
|
|
Label numberLabel = new Label("请输入进制数:");
|
|
|
|
|
GridPane.setConstraints(numberLabel, 0, 1);
|
|
|
|
|
TextField numberInput = new TextField();
|
|
|
|
|
GridPane.setConstraints(numberInput, 1, 1);
|
|
|
|
|
|
|
|
|
|
// 目标进制标签和输入框
|
|
|
|
|
Label targetBaseLabel = new Label("目标进制 (2-16):");
|
|
|
|
|
GridPane.setConstraints(targetBaseLabel, 0, 2);
|
|
|
|
|
TextField targetBaseInput = new TextField();
|
|
|
|
|
GridPane.setConstraints(targetBaseInput, 1, 2);
|
|
|
|
|
|
|
|
|
|
// 转换按钮
|
|
|
|
|
Button convertButton = new Button("转换");
|
|
|
|
|
GridPane.setConstraints(convertButton, 1, 3);
|
|
|
|
|
|
|
|
|
|
// 结果标签
|
|
|
|
|
Label resultLabel = new Label();
|
|
|
|
|
GridPane.setConstraints(resultLabel, 1, 4);
|
|
|
|
|
|
|
|
|
|
// 设置按钮事件处理
|
|
|
|
|
convertButton.setOnAction(e -> {
|
|
|
|
|
try {
|
|
|
|
|
// 获取用户输入
|
|
|
|
|
int originalBase = Integer.parseInt(originalBaseInput.getText());
|
|
|
|
|
String number = numberInput.getText().toUpperCase();
|
|
|
|
|
int targetBase = Integer.parseInt(targetBaseInput.getText());
|
|
|
|
|
|
|
|
|
|
// 检查输入的进制范围是否合法
|
|
|
|
|
if (originalBase < 2 || originalBase > 16 || targetBase < 2 || targetBase > 16) {
|
|
|
|
|
resultLabel.setText("进制范围必须在2到16之间!");
|
|
|
|
|
} else {
|
|
|
|
|
// 执行进制转换
|
|
|
|
|
int decimalNumber = convertToDecimal(number, originalBase);
|
|
|
|
|
String result = convertFromDecimal(decimalNumber, targetBase);
|
|
|
|
|
resultLabel.setText("转换后的结果: " + result);
|
|
|
|
|
}
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
resultLabel.setText("输入格式错误,请检查!");
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
resultLabel.setText("转换失败: " + ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 将所有控件添加到布局中
|
|
|
|
|
grid.getChildren().addAll(originalBaseLabel, originalBaseInput, numberLabel, numberInput, targetBaseLabel, targetBaseInput, convertButton, resultLabel);
|
|
|
|
|
|
|
|
|
|
// 创建场景并显示
|
|
|
|
|
Scene scene = new Scene(grid, 400, 250);
|
|
|
|
|
primaryStage.setScene(scene);
|
|
|
|
|
primaryStage.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
launch(args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|