parent
42d687031e
commit
53710003cf
@ -0,0 +1,125 @@
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class TestController extends Application {
|
||||
|
||||
private TextField inputTextField;
|
||||
private Label resultLabel;
|
||||
private ToggleGroup sourceBaseGroup;
|
||||
private ToggleGroup targetBaseGroup;
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
// 创建组件
|
||||
Label sourceBaseLabel = new Label("数值进制:");
|
||||
sourceBaseGroup = new ToggleGroup();
|
||||
RadioButton rb2 = new RadioButton("2进制");
|
||||
RadioButton rb8 = new RadioButton("8进制");
|
||||
RadioButton rb10 = new RadioButton("10进制");
|
||||
RadioButton rb16 = new RadioButton("16进制");
|
||||
rb10.setSelected(true); // 默认选择10进制
|
||||
|
||||
rb2.setToggleGroup(sourceBaseGroup);
|
||||
rb8.setToggleGroup(sourceBaseGroup);
|
||||
rb10.setToggleGroup(sourceBaseGroup);
|
||||
rb16.setToggleGroup(sourceBaseGroup);
|
||||
|
||||
Label inputLabel = new Label("输入数字:");
|
||||
inputTextField = new TextField();
|
||||
Label targetBaseLabel = new Label("结果进制:");
|
||||
targetBaseGroup = new ToggleGroup();
|
||||
RadioButton rb2Result = new RadioButton("2进制");
|
||||
RadioButton rb8Result = new RadioButton("8进制");
|
||||
RadioButton rb10Result = new RadioButton("10进制");
|
||||
RadioButton rb16Result = new RadioButton("16进制");
|
||||
rb16Result.setSelected(true); // 默认选择16进制
|
||||
|
||||
rb2Result.setToggleGroup(targetBaseGroup);
|
||||
rb8Result.setToggleGroup(targetBaseGroup);
|
||||
rb10Result.setToggleGroup(targetBaseGroup);
|
||||
rb16Result.setToggleGroup(targetBaseGroup);
|
||||
|
||||
Button convertButton = new Button("转换");
|
||||
resultLabel = new Label("转换结果:");
|
||||
|
||||
// 设置布局
|
||||
GridPane gridPane = new GridPane();
|
||||
gridPane.setPadding(new Insets(10));
|
||||
gridPane.setVgap(10);
|
||||
gridPane.setHgap(10);
|
||||
|
||||
gridPane.add(sourceBaseLabel, 0, 0);
|
||||
gridPane.add(rb2, 1, 0);
|
||||
gridPane.add(rb8, 2, 0);
|
||||
gridPane.add(rb10, 3, 0);
|
||||
gridPane.add(rb16, 4, 0);
|
||||
gridPane.add(inputLabel, 0, 1);
|
||||
gridPane.add(inputTextField, 1, 1, 4, 1);
|
||||
gridPane.add(targetBaseLabel, 0, 2);
|
||||
gridPane.add(rb2Result, 1, 2);
|
||||
gridPane.add(rb8Result, 2, 2);
|
||||
gridPane.add(rb10Result, 3, 2);
|
||||
gridPane.add(rb16Result, 4, 2);
|
||||
gridPane.add(convertButton, 0, 3);
|
||||
gridPane.add(resultLabel, 1, 3, 4, 1);
|
||||
|
||||
// 设置场景
|
||||
Scene scene = new Scene(gridPane, 600, 200);
|
||||
|
||||
// 设置窗口
|
||||
primaryStage.setTitle("进制转换工具");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
|
||||
// 添加事件监听器
|
||||
convertButton.setOnAction(event -> {
|
||||
int sourceBase = getSelectedBase(sourceBaseGroup);
|
||||
int targetBase = getSelectedBase(targetBaseGroup);
|
||||
String inputValue = inputTextField.getText();
|
||||
|
||||
try {
|
||||
int decimalValue = toDecimal(inputValue, sourceBase);
|
||||
String result = fromDecimal(decimalValue, targetBase);
|
||||
resultLabel.setText("转换结果: " + result);
|
||||
} catch (NumberFormatException e) {
|
||||
resultLabel.setText("输入错误,请检查输入格式!");
|
||||
} catch (IllegalArgumentException e) {
|
||||
resultLabel.setText(e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private int getSelectedBase(ToggleGroup group) {
|
||||
RadioButton selected = (RadioButton) group.getSelectedToggle();
|
||||
switch (selected.getText()) {
|
||||
case "2进制":
|
||||
return 2;
|
||||
case "8进制":
|
||||
return 8;
|
||||
case "10进制":
|
||||
return 10;
|
||||
case "16进制":
|
||||
return 16;
|
||||
default:
|
||||
throw new IllegalArgumentException("未选择有效进制");
|
||||
}
|
||||
}
|
||||
|
||||
// 将字符串转换为十进制
|
||||
public static int toDecimal(String value, int base) {
|
||||
return Integer.parseInt(value, base);
|
||||
}
|
||||
|
||||
// 将十进制转换为指定进制
|
||||
public static String fromDecimal(int value, int base) {
|
||||
return Integer.toString(value, base).toUpperCase();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import java.lang.*?>
|
||||
<?import java.util.*?>
|
||||
<?import javafx.scene.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
|
||||
<AnchorPane xmlns="http://javafx.com/javafx"
|
||||
xmlns:fx="http://javafx.com/fxml"
|
||||
fx:controller="TestController"
|
||||
prefHeight="400.0" prefWidth="600.0">
|
||||
</AnchorPane>
|
Loading…
Reference in new issue