|
|
|
@ -12,21 +12,17 @@ public class test2 extends Application {
|
|
|
|
|
public void start(Stage primaryStage) {
|
|
|
|
|
// 创建VBox作为垂直布局容器
|
|
|
|
|
VBox vBox = new VBox(10.0);
|
|
|
|
|
|
|
|
|
|
// 创建提示标签
|
|
|
|
|
Label promptLabel = new Label("请输入一个十进制数字");
|
|
|
|
|
promptLabel.setStyle("-fx-font-weight: bold;"); // 设置字体为粗体
|
|
|
|
|
|
|
|
|
|
// 创建输入框并设置提示文本
|
|
|
|
|
TextField inputField = new TextField();
|
|
|
|
|
inputField.setPromptText("输入数字");
|
|
|
|
|
|
|
|
|
|
// 创建标签用于显示转换结果
|
|
|
|
|
Label binaryLabel = new Label();
|
|
|
|
|
Label octalLabel = new Label();
|
|
|
|
|
Label hexLabel = new Label();
|
|
|
|
|
Label base6Label = new Label();
|
|
|
|
|
|
|
|
|
|
// 创建转换按钮并设置事件处理器
|
|
|
|
|
Button convertButton = new Button("转换");
|
|
|
|
|
convertButton.setOnAction(e -> {
|
|
|
|
@ -35,13 +31,17 @@ public class test2 extends Application {
|
|
|
|
|
int number;
|
|
|
|
|
if (inputText.matches("-?\\d+")) { // 十进制
|
|
|
|
|
number = Integer.parseInt(inputText);
|
|
|
|
|
} else if (inputText.matches("-?[0-7]+")) { // 八进制
|
|
|
|
|
}
|
|
|
|
|
else if (inputText.matches("-?[0-7]+")) { // 八进制
|
|
|
|
|
number = Integer.parseInt(inputText, 8);
|
|
|
|
|
} else if (inputText.matches("-?[01]+")) { // 二进制
|
|
|
|
|
}
|
|
|
|
|
else if (inputText.matches("-?[01]+")) { // 二进制
|
|
|
|
|
number = Integer.parseInt(inputText, 2);
|
|
|
|
|
} else if (inputText.matches("-?[\\da-fA-F]+")) { // 十六进制
|
|
|
|
|
}
|
|
|
|
|
else if (inputText.matches("-?[\\da-fA-F]+")) { // 十六进制
|
|
|
|
|
number = Integer.parseInt(inputText, 16);
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw new NumberFormatException();
|
|
|
|
|
}
|
|
|
|
|
binaryLabel.setText(String.format("二进制: %s", Integer.toBinaryString(number).toUpperCase()));
|
|
|
|
@ -49,7 +49,8 @@ public class test2 extends Application {
|
|
|
|
|
hexLabel.setText(String.format("十六进制: %s", Integer.toHexString(number).toUpperCase()));
|
|
|
|
|
base6Label.setText(String.format("六进制: %s", toBase6String(number)));
|
|
|
|
|
vBox.getChildren().add(base6Label);
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
}
|
|
|
|
|
catch (NumberFormatException ex) {
|
|
|
|
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
|
|
|
|
alert.setTitle("输入错误");
|
|
|
|
|
alert.setHeaderText("无效输入");
|
|
|
|
@ -57,38 +58,33 @@ public class test2 extends Application {
|
|
|
|
|
alert.showAndWait();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 将组件添加到布局中
|
|
|
|
|
vBox.getChildren().addAll(promptLabel, inputField, convertButton, binaryLabel, octalLabel, hexLabel);
|
|
|
|
|
|
|
|
|
|
// 设置场景和舞台
|
|
|
|
|
Scene scene = new Scene(vBox, 300.0, 200.0);
|
|
|
|
|
primaryStage.setTitle("进制转换器");
|
|
|
|
|
primaryStage.setScene(scene);
|
|
|
|
|
primaryStage.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将十进制数转换为六进制字符串的方法
|
|
|
|
|
private String toBase6String(int number) {
|
|
|
|
|
if (number == 0) {
|
|
|
|
|
return "0";
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
char[] digits = new char[]{'0', '1', '2', '3', '4', '5'};
|
|
|
|
|
boolean isNegative = number < 0;
|
|
|
|
|
if (isNegative) {
|
|
|
|
|
number = -number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (number > 0) {
|
|
|
|
|
sb.insert(0, digits[number % 6]);
|
|
|
|
|
number /= 6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isNegative) {
|
|
|
|
|
sb.insert(0, '-');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|