parent
67f9d0538d
commit
9103c2e9d5
@ -0,0 +1,89 @@
|
|||||||
|
package czy.czy;
|
||||||
|
|
||||||
|
import javafx.application.Application;
|
||||||
|
import javafx.geometry.Insets;
|
||||||
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.control.Button;
|
||||||
|
import javafx.scene.control.Label;
|
||||||
|
import javafx.scene.control.TextField;
|
||||||
|
import javafx.scene.layout.GridPane;
|
||||||
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
public class BaseConverterApp extends Application {
|
||||||
|
|
||||||
|
private TextField fromBaseField;
|
||||||
|
private TextField toBaseField;
|
||||||
|
private TextField numStrField;
|
||||||
|
private Label resultLabel;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Stage primaryStage) {
|
||||||
|
primaryStage.setTitle("进制转换器");
|
||||||
|
|
||||||
|
GridPane grid = new GridPane();
|
||||||
|
grid.setPadding(new Insets(10, 10, 10, 10));
|
||||||
|
grid.setVgap(8);
|
||||||
|
grid.setHgap(10);
|
||||||
|
|
||||||
|
Label fromBaseLabel = new Label("原始进制:");
|
||||||
|
fromBaseField = new TextField();
|
||||||
|
fromBaseField.setPromptText("2-16");
|
||||||
|
GridPane.setConstraints(fromBaseLabel, 0, 0);
|
||||||
|
GridPane.setConstraints(fromBaseField, 1, 0);
|
||||||
|
|
||||||
|
Label toBaseLabel = new Label("目标进制:");
|
||||||
|
toBaseField = new TextField();
|
||||||
|
toBaseField.setPromptText("2-16");
|
||||||
|
GridPane.setConstraints(toBaseLabel, 0, 1);
|
||||||
|
GridPane.setConstraints(toBaseField, 1, 1);
|
||||||
|
|
||||||
|
Label numStrLabel = new Label("原始数值:");
|
||||||
|
numStrField = new TextField();
|
||||||
|
GridPane.setConstraints(numStrLabel, 0, 2);
|
||||||
|
GridPane.setConstraints(numStrField, 1, 2);
|
||||||
|
|
||||||
|
Button convertButton = new Button("转换");
|
||||||
|
GridPane.setConstraints(convertButton, 1, 3);
|
||||||
|
|
||||||
|
resultLabel = new Label();
|
||||||
|
GridPane.setConstraints(resultLabel, 1, 4);
|
||||||
|
|
||||||
|
grid.getChildren().addAll(fromBaseLabel, fromBaseField, toBaseLabel, toBaseField, numStrLabel, numStrField, convertButton, resultLabel);
|
||||||
|
|
||||||
|
Scene scene = new Scene(grid, 300, 200);
|
||||||
|
primaryStage.setScene(scene);
|
||||||
|
primaryStage.show();
|
||||||
|
|
||||||
|
convertButton.setOnAction(event -> {
|
||||||
|
try {
|
||||||
|
int fromBase = Integer.parseInt(fromBaseField.getText());
|
||||||
|
int toBase = Integer.parseInt(toBaseField.getText());
|
||||||
|
String numStr = numStrField.getText();
|
||||||
|
String result = conv(numStr, fromBase, toBase);
|
||||||
|
resultLabel.setText("转换后的结果为: " + result);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
resultLabel.setText(e.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
launch(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String conv(String numStr, int fromBase, int toBase) {
|
||||||
|
if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) {
|
||||||
|
throw new IllegalArgumentException("Base must be between 2 and 16.");
|
||||||
|
}
|
||||||
|
|
||||||
|
BigInteger decimalValue = new BigInteger(numStr, fromBase);
|
||||||
|
|
||||||
|
if (toBase == 10) {
|
||||||
|
return decimalValue.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return decimalValue.toString(toBase).toUpperCase();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue