You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.5 KiB
101 lines
3.5 KiB
import javafx.application.Application;
|
|
import javafx.scene.Scene;
|
|
import javafx.scene.control.Alert;
|
|
import javafx.scene.control.Button;
|
|
import javafx.scene.control.Label;
|
|
import javafx.scene.control.TextField;
|
|
import javafx.scene.layout.VBox;
|
|
import javafx.stage.Stage;
|
|
|
|
public class test2 extends Application {
|
|
@Override
|
|
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("输入数字");
|
|
|
|
// 创建输入框并设置提示文本
|
|
TextField fromBaseField = new TextField();
|
|
fromBaseField.setPromptText("输入原始进制");
|
|
|
|
// 创建输入框并设置提示文本
|
|
TextField toBaseField = new TextField();
|
|
toBaseField.setPromptText("输入目标进制");
|
|
|
|
// 创建标签用于显示转换结果
|
|
Label resultLabel = new Label();
|
|
|
|
// 创建按钮
|
|
Button convertButton = new Button("转换");
|
|
|
|
// 更新标签的函数
|
|
final TextField finalInputField = inputField;
|
|
final TextField finalFromBaseField = fromBaseField;
|
|
final TextField finalToBaseField = toBaseField;
|
|
|
|
convertButton.setOnAction(e -> {
|
|
try {
|
|
String inputText = finalInputField.getText();
|
|
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.showAndWait();
|
|
}
|
|
});
|
|
|
|
// 将组件添加到布局中
|
|
vBox.getChildren().addAll(promptLabel, inputField, fromBaseField, toBaseField, convertButton, resultLabel);
|
|
|
|
// 设置场景和舞台
|
|
Scene scene = new Scene(vBox, 300.0, 200.0);
|
|
primaryStage.setTitle("进制转换器");
|
|
primaryStage.setScene(scene);
|
|
primaryStage.show();
|
|
}
|
|
|
|
// 将十进制数转换为指定进制的字符串
|
|
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', '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 % base]);
|
|
number /= base;
|
|
}
|
|
if (isNegative) {
|
|
sb.insert(0, '-');
|
|
}
|
|
return sb.toString();
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
launch(args);
|
|
}
|
|
}
|