diff --git a/out/production/lesson1/text1.class b/out/production/lesson1/text1.class index 559205f..0028773 100644 Binary files a/out/production/lesson1/text1.class and b/out/production/lesson1/text1.class differ diff --git a/test2.java b/test2.java new file mode 100644 index 0000000..2df9642 --- /dev/null +++ b/test2.java @@ -0,0 +1,99 @@ +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("输入数字"); + + // 创建标签用于显示转换结果 + Label binaryLabel = new Label(); + 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(); + } + 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); + + // 设置场景和舞台 + 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 { + 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(); + } + } + + public static void main(String[] args) { + launch(args); + } +} \ No newline at end of file