|
|
package Calculator.jzzh;
|
|
|
|
|
|
import javafx.application.Application;
|
|
|
import javafx.geometry.Insets;
|
|
|
import javafx.geometry.Pos;
|
|
|
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;
|
|
|
|
|
|
public class CalculatorAPP extends Application {
|
|
|
|
|
|
private TextField numberField;
|
|
|
private TextField fromBaseField;
|
|
|
private TextField toBaseField;
|
|
|
private Label resultLabel;
|
|
|
|
|
|
@Override
|
|
|
public void start(Stage primaryStage) {
|
|
|
// 创建UI组件
|
|
|
Label numberLabel = new Label("请输入要转换的数:");
|
|
|
numberField = new TextField();
|
|
|
|
|
|
Label fromBaseLabel = new Label("请输入源进制(2-16):");
|
|
|
fromBaseField = new TextField();
|
|
|
|
|
|
Label toBaseLabel = new Label("请输入目标进制(2-16):");
|
|
|
toBaseField = new TextField();
|
|
|
|
|
|
Button convertButton = new Button("转换");
|
|
|
resultLabel = new Label();
|
|
|
|
|
|
// 创建布局
|
|
|
GridPane gridPane = new GridPane();
|
|
|
gridPane.setAlignment(Pos.CENTER);
|
|
|
gridPane.setHgap(10);
|
|
|
gridPane.setVgap(10);
|
|
|
gridPane.setPadding(new Insets(20));
|
|
|
|
|
|
// 添加组件到布局
|
|
|
gridPane.add(numberLabel, 0, 0);
|
|
|
gridPane.add(numberField, 1, 0);
|
|
|
gridPane.add(fromBaseLabel, 0, 1);
|
|
|
gridPane.add(fromBaseField, 1, 1);
|
|
|
gridPane.add(toBaseLabel, 0, 2);
|
|
|
gridPane.add(toBaseField, 1, 2);
|
|
|
gridPane.add(convertButton, 0, 3);
|
|
|
gridPane.add(resultLabel, 1, 3);
|
|
|
|
|
|
// 为按钮添加事件监听器
|
|
|
convertButton.setOnAction(e -> {
|
|
|
try {
|
|
|
String num = numberField.getText().trim();
|
|
|
int fromBase = Integer.parseInt(fromBaseField.getText().trim());
|
|
|
int toBase = Integer.parseInt(toBaseField.getText().trim());
|
|
|
|
|
|
// 验证输入的有效性
|
|
|
if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) {
|
|
|
resultLabel.setText("进制范围应在2到16之间。");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 调用转换方法
|
|
|
String converted = convertBase(num, fromBase, toBase);
|
|
|
|
|
|
// 更新结果显示
|
|
|
resultLabel.setText(String.format("%s (%d进制) 转换为 (%d进制) 是 %s", num, fromBase, toBase, converted));
|
|
|
} catch (NumberFormatException ex) {
|
|
|
resultLabel.setText("输入有误,请检查输入是否正确。");
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 创建场景
|
|
|
Scene scene = new Scene(gridPane, 400, 300);
|
|
|
|
|
|
// 设置舞台
|
|
|
primaryStage.setTitle("进制转换器");
|
|
|
primaryStage.setScene(scene);
|
|
|
primaryStage.show();
|
|
|
}
|
|
|
|
|
|
public static String convertBase(String num, int fromBase, int toBase) {
|
|
|
// 将给定的fromBase进制数转换为十进制数
|
|
|
int decimalNum = Integer.parseInt(num, fromBase);
|
|
|
|
|
|
// 定义字符集以表示大于10的进制数
|
|
|
char[] digits = "0123456789ABCDEF".toCharArray();
|
|
|
|
|
|
// 如果目标进制是10,则直接返回decimalNum
|
|
|
if (toBase == 10) {
|
|
|
return String.valueOf(decimalNum);
|
|
|
}
|
|
|
|
|
|
// 否则,从十进制转换为目标进制
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
while (decimalNum > 0) {
|
|
|
int remainder = decimalNum % toBase;
|
|
|
result.insert(0, digits[remainder]);
|
|
|
decimalNum /= toBase;
|
|
|
}
|
|
|
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
launch(args);
|
|
|
}
|
|
|
}
|