|
|
|
@ -0,0 +1,128 @@
|
|
|
|
|
import javafx.application.Application;
|
|
|
|
|
import javafx.geometry.Insets;
|
|
|
|
|
import javafx.geometry.Pos;
|
|
|
|
|
import javafx.scene.Scene;
|
|
|
|
|
import javafx.scene.control.Alert;
|
|
|
|
|
import javafx.scene.control.Alert.AlertType;
|
|
|
|
|
import javafx.scene.control.Button;
|
|
|
|
|
import javafx.scene.control.Label;
|
|
|
|
|
import javafx.scene.control.TextField;
|
|
|
|
|
import javafx.scene.layout.GridPane;
|
|
|
|
|
import javafx.stage.Stage;
|
|
|
|
|
|
|
|
|
|
public class jzzh extends Application {
|
|
|
|
|
|
|
|
|
|
private TextField sourceBaseField;
|
|
|
|
|
private TextField targetBaseField;
|
|
|
|
|
private TextField sourceNumberField;
|
|
|
|
|
private TextField resultField;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void start(Stage primaryStage) {
|
|
|
|
|
primaryStage.setTitle("进制转换器(2-16)");
|
|
|
|
|
|
|
|
|
|
GridPane grid = new GridPane();
|
|
|
|
|
grid.setAlignment(Pos.CENTER);
|
|
|
|
|
grid.setHgap(10);
|
|
|
|
|
grid.setVgap(10);
|
|
|
|
|
grid.setPadding(new Insets(25, 25, 25, 25));
|
|
|
|
|
|
|
|
|
|
Label sourceBaseLabel = new Label("源进制:");
|
|
|
|
|
GridPane.setConstraints(sourceBaseLabel, 0, 0);
|
|
|
|
|
sourceBaseField = new TextField();
|
|
|
|
|
GridPane.setConstraints(sourceBaseField, 1, 0);
|
|
|
|
|
|
|
|
|
|
Label sourceNumberLabel = new Label("源数:");
|
|
|
|
|
GridPane.setConstraints(sourceNumberLabel, 0, 1);
|
|
|
|
|
sourceNumberField = new TextField();
|
|
|
|
|
GridPane.setConstraints(sourceNumberField, 1, 1);
|
|
|
|
|
|
|
|
|
|
Label targetBaseLabel = new Label("目标进制:");
|
|
|
|
|
GridPane.setConstraints(targetBaseLabel, 0, 2);
|
|
|
|
|
targetBaseField = new TextField();
|
|
|
|
|
GridPane.setConstraints(targetBaseField, 1, 2);
|
|
|
|
|
|
|
|
|
|
Label resultLabel = new Label("转换后的数:");
|
|
|
|
|
GridPane.setConstraints(resultLabel, 0, 3);
|
|
|
|
|
resultField = new TextField();
|
|
|
|
|
resultField.setEditable(false); // 结果字段不可编辑
|
|
|
|
|
GridPane.setConstraints(resultField, 1, 3);
|
|
|
|
|
|
|
|
|
|
Button convertButton = new Button("转换");
|
|
|
|
|
GridPane.setConstraints(convertButton, 1, 4);
|
|
|
|
|
|
|
|
|
|
convertButton.setOnAction(e -> {
|
|
|
|
|
try {
|
|
|
|
|
int sourceBase = Integer.parseInt(sourceBaseField.getText());
|
|
|
|
|
int targetBase = Integer.parseInt(targetBaseField.getText());
|
|
|
|
|
String sourceNumber = sourceNumberField.getText();
|
|
|
|
|
|
|
|
|
|
if (sourceBase < 2 || sourceBase > 16 || targetBase < 2 || targetBase > 16) {
|
|
|
|
|
throw new NumberFormatException("进制必须在2到16之间!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int decimalValue = toDecimal(sourceNumber, sourceBase);
|
|
|
|
|
String targetNumber = fromDecimal(decimalValue, targetBase);
|
|
|
|
|
|
|
|
|
|
resultField.setText(targetNumber);
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
showAlert(AlertType.ERROR, "输入错误", ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
grid.getChildren().addAll(sourceBaseLabel, sourceBaseField, sourceNumberLabel, sourceNumberField,
|
|
|
|
|
targetBaseLabel, targetBaseField, resultLabel, resultField, convertButton);
|
|
|
|
|
|
|
|
|
|
Scene scene = new Scene(grid, 300, 200);
|
|
|
|
|
|
|
|
|
|
primaryStage.setScene(scene);
|
|
|
|
|
primaryStage.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将任意进制的字符串转换为十进制整数(与之前的实现相同)
|
|
|
|
|
private int toDecimal(String number, int base) {
|
|
|
|
|
int decimalValue = 0;
|
|
|
|
|
int power = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = number.length() - 1; i >= 0; i--) {
|
|
|
|
|
char digit = number.charAt(i);
|
|
|
|
|
int value = Character.digit(digit, base); // 使用Java内置的字符到数字的转换
|
|
|
|
|
if (value == -1) {
|
|
|
|
|
throw new NumberFormatException("无效的字符: " + digit);
|
|
|
|
|
}
|
|
|
|
|
decimalValue += value * Math.pow(base, power);
|
|
|
|
|
power++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decimalValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将十进制整数转换为任意进制的字符串(与之前的实现相同)
|
|
|
|
|
private String fromDecimal(int decimalValue, int base) {
|
|
|
|
|
if (decimalValue == 0) {
|
|
|
|
|
return "0";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
while (decimalValue > 0) {
|
|
|
|
|
int remainder = decimalValue % base;
|
|
|
|
|
result.insert(0, Character.forDigit(remainder, base)); // 使用Java内置的数字到字符的转换
|
|
|
|
|
decimalValue /= base;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 显示警告对话框
|
|
|
|
|
private void showAlert(AlertType alertType, String title, String content) {
|
|
|
|
|
Alert alert = new Alert(alertType);
|
|
|
|
|
alert.setTitle(title);
|
|
|
|
|
alert.setContentText(content);
|
|
|
|
|
alert.setHeaderText(null);
|
|
|
|
|
alert.showAndWait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
launch(args);
|
|
|
|
|
}
|
|
|
|
|
}
|