You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.5 KiB
43 lines
1.5 KiB
import javafx.fxml.FXML;
|
|
import javafx.scene.control.Label;
|
|
import javafx.scene.control.TextField;
|
|
|
|
public class BaseConverterController {
|
|
|
|
@FXML
|
|
private TextField txtNumber; // 输入数字的文本框
|
|
@FXML
|
|
private TextField txtFromBase; // 输入源进制的文本框
|
|
@FXML
|
|
private TextField txtToBase; // 输入目标进制的文本框
|
|
@FXML
|
|
private Label lblResult; // 显示结果的标签
|
|
|
|
// 按钮点击事件处理
|
|
@FXML
|
|
protected void handleConvert() {
|
|
try {
|
|
String number = txtNumber.getText();
|
|
int fromBase = Integer.parseInt(txtFromBase.getText());
|
|
int toBase = Integer.parseInt(txtToBase.getText());
|
|
|
|
// 验证进制范围
|
|
if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) {
|
|
lblResult.setText("无效的进制 (2 ≤ R ≤ 16)");
|
|
} else {
|
|
// 进行进制转换
|
|
String result = convertBase(number, fromBase, toBase);
|
|
lblResult.setText("结果: " + result);
|
|
}
|
|
} catch (NumberFormatException ex) {
|
|
lblResult.setText("输入无效,请输入有效的数字。");
|
|
}
|
|
}
|
|
|
|
// 进制转换的辅助方法
|
|
private String convertBase(String number, int fromBase, int toBase) {
|
|
int decimalValue = Integer.parseInt(number, fromBase); // 转换为十进制
|
|
return Integer.toString(decimalValue, toBase).toUpperCase(); // 转换为目标进制
|
|
}
|
|
}
|