master
zpj 1 month ago
parent c73708289c
commit 3109505073

@ -10,11 +10,11 @@ import javafx.stage.Stage;
public class test2 extends Application {
@Override
public void start(Stage primaryStage) {
// 创建VBox作为垂直布局容器15
// 创建VBox作为垂直布局容器
VBox vBox = new VBox(10.0);
// 创建提示标签
Label promptLabel = new Label("请输入一个十进制数");
promptLabel.setStyle("-fx-font-weight: bold;"); // 设置字体为粗体
promptLabel.setStyle("-fx-font-weight: bold;");
// 创建输入框并设置提示文本
TextField inputField = new TextField();
inputField.setPromptText("输入数字");
@ -23,55 +23,60 @@ public class test2 extends Application {
Label octalLabel = new Label();
Label hexLabel = new Label();
Label base6Label = new Label();
// 创建转换按钮并设置事件处理器
Button convertButton = new Button("转换");
convertButton.setOnAction(e -> {
try {
String inputText = inputField.getText();
int number;
if (inputText.matches("-?\\d+")) { // 十进制
number = Integer.parseInt(inputText);
}
else if (inputText.matches("-?[0-7]+")) { // 八进制
number = Integer.parseInt(inputText, 8);
}
else if (inputText.matches("-?[01]+")) { // 二进制
number = Integer.parseInt(inputText, 2);
}
else if (inputText.matches("-?[\\da-fA-F]+")) { // 十六进制
number = Integer.parseInt(inputText, 16);
}
else {
throw new NumberFormatException();
// 创建按钮
Button binaryButton = new Button("二进制");
Button octalButton = new Button("八进制");
Button hexButton = new Button("十六进制");
Button base6Button = 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 -> {
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;
}
} catch (NumberFormatException ex) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("输入错误");
alert.setHeaderText("无效输入");
alert.setContentText("请输入一个有效的数字。");
alert.showAndWait();
}
binaryLabel.setText(String.format("二进制: %s", Integer.toBinaryString(number).toUpperCase()));
octalLabel.setText(String.format("八进制: %s", Integer.toOctalString(number)));
hexLabel.setText(String.format("十六进制: %s", Integer.toHexString(number).toUpperCase()));
base6Label.setText(String.format("六进制: %s", toBase6String(number)));
vBox.getChildren().add(base6Label);
}
catch (NumberFormatException ex) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("输入错误");
alert.setHeaderText("无效输入");
alert.setContentText("请输入一个有效的数字。");
alert.showAndWait();
}
});
});
}
// 将组件添加到布局中
vBox.getChildren().addAll(promptLabel, inputField, convertButton, binaryLabel, octalLabel, hexLabel);
vBox.getChildren().addAll(promptLabel, inputField, binaryButton, octalButton, hexButton, base6Button, binaryLabel, octalLabel, hexLabel, base6Label);
// 设置场景和舞台
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;

Loading…
Cancel
Save