|
|
|
@ -12,59 +12,59 @@ public class test2 extends Application {
|
|
|
|
|
public void start(Stage primaryStage) {
|
|
|
|
|
// 创建VBox作为垂直布局容器
|
|
|
|
|
VBox vBox = new VBox(10.0);
|
|
|
|
|
|
|
|
|
|
// 创建提示标签
|
|
|
|
|
Label promptLabel = new Label("请输入一个十进制数");
|
|
|
|
|
Label promptLabel = new Label("请输入一个数字");
|
|
|
|
|
promptLabel.setStyle("-fx-font-weight: bold;");
|
|
|
|
|
|
|
|
|
|
// 创建输入框并设置提示文本
|
|
|
|
|
TextField inputField = new TextField();
|
|
|
|
|
inputField.setPromptText("输入数字");
|
|
|
|
|
|
|
|
|
|
// 创建输入框并设置提示文本
|
|
|
|
|
TextField fromBaseField = new TextField();
|
|
|
|
|
fromBaseField.setPromptText("输入原始进制");
|
|
|
|
|
|
|
|
|
|
// 创建输入框并设置提示文本
|
|
|
|
|
TextField toBaseField = new TextField();
|
|
|
|
|
toBaseField.setPromptText("输入目标进制");
|
|
|
|
|
|
|
|
|
|
// 创建标签用于显示转换结果
|
|
|
|
|
Label binaryLabel = new Label();
|
|
|
|
|
Label octalLabel = new Label();
|
|
|
|
|
Label hexLabel = new Label();
|
|
|
|
|
Label base6Label = new Label();
|
|
|
|
|
Label resultLabel = new Label();
|
|
|
|
|
|
|
|
|
|
// 创建按钮
|
|
|
|
|
Button binaryButton = new Button("二进制");
|
|
|
|
|
Button octalButton = new Button("八进制");
|
|
|
|
|
Button hexButton = new Button("十六进制");
|
|
|
|
|
Button base6Button = new Button("六进制");
|
|
|
|
|
Button convertButton = new Button("转换");
|
|
|
|
|
|
|
|
|
|
// 更新标签的函数
|
|
|
|
|
final TextField finalInputField = inputField;
|
|
|
|
|
Button[] buttons = {binaryButton, octalButton, hexButton, base6Button};
|
|
|
|
|
Label[] labels = {binaryLabel, octalLabel, hexLabel, base6Label};
|
|
|
|
|
for (int i = 0; i < buttons.length; i++) {
|
|
|
|
|
final int index = i;
|
|
|
|
|
buttons[i].setOnAction(e -> {
|
|
|
|
|
final TextField finalFromBaseField = fromBaseField;
|
|
|
|
|
final TextField finalToBaseField = toBaseField;
|
|
|
|
|
|
|
|
|
|
convertButton.setOnAction(e -> {
|
|
|
|
|
try {
|
|
|
|
|
String inputText = finalInputField.getText();
|
|
|
|
|
int number = Integer.parseInt(inputText);
|
|
|
|
|
switch (index) {
|
|
|
|
|
case 0:
|
|
|
|
|
labels[index].setText(String.format("二进制: %s", Integer.toBinaryString(number).toUpperCase()));
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
labels[index].setText(String.format("八进制: %s", Integer.toOctalString(number)));
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
labels[index].setText(String.format("十六进制: %s", Integer.toHexString(number).toUpperCase()));
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
labels[index].setText(String.format("六进制: %s", toBase6String(number)));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
int fromBase = Integer.parseInt(finalFromBaseField.getText());
|
|
|
|
|
int toBase = Integer.parseInt(finalToBaseField.getText());
|
|
|
|
|
|
|
|
|
|
// 将输入的数字从原始进制转换为十进制
|
|
|
|
|
int decimalNumber = Integer.parseInt(inputText, fromBase);
|
|
|
|
|
|
|
|
|
|
// 将十进制数字转换为目标进制
|
|
|
|
|
String result = toBaseString(decimalNumber, toBase);
|
|
|
|
|
|
|
|
|
|
resultLabel.setText(String.format("%d 进制: %s", toBase, result));
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
|
|
|
|
alert.setTitle("输入错误");
|
|
|
|
|
alert.setHeaderText("无效输入");
|
|
|
|
|
alert.setContentText("请输入一个有效的数字。");
|
|
|
|
|
alert.setContentText("请输入有效的数字和进制。");
|
|
|
|
|
alert.showAndWait();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将组件添加到布局中
|
|
|
|
|
vBox.getChildren().addAll(promptLabel, inputField, binaryButton, octalButton, hexButton, base6Button, binaryLabel, octalLabel, hexLabel, base6Label);
|
|
|
|
|
vBox.getChildren().addAll(promptLabel, inputField, fromBaseField, toBaseField, convertButton, resultLabel);
|
|
|
|
|
|
|
|
|
|
// 设置场景和舞台
|
|
|
|
|
Scene scene = new Scene(vBox, 300.0, 200.0);
|
|
|
|
|
primaryStage.setTitle("进制转换器");
|
|
|
|
@ -72,20 +72,20 @@ public class test2 extends Application {
|
|
|
|
|
primaryStage.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将十进制数转换为六进制字符串的方法
|
|
|
|
|
private String toBase6String(int number) {
|
|
|
|
|
// 将十进制数转换为指定进制的字符串
|
|
|
|
|
private String toBaseString(int number, int base) {
|
|
|
|
|
if (number == 0) {
|
|
|
|
|
return "0";
|
|
|
|
|
} else {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
char[] digits = new char[]{'0', '1', '2', '3', '4', '5'};
|
|
|
|
|
char[] digits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
|
|
|
|
boolean isNegative = number < 0;
|
|
|
|
|
if (isNegative) {
|
|
|
|
|
number = -number;
|
|
|
|
|
}
|
|
|
|
|
while (number > 0) {
|
|
|
|
|
sb.insert(0, digits[number % 6]);
|
|
|
|
|
number /= 6;
|
|
|
|
|
sb.insert(0, digits[number % base]);
|
|
|
|
|
number /= base;
|
|
|
|
|
}
|
|
|
|
|
if (isNegative) {
|
|
|
|
|
sb.insert(0, '-');
|
|
|
|
|