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.
55 lines
1.9 KiB
55 lines
1.9 KiB
package java6239.uml;
|
|
|
|
import javafx.event.ActionEvent;
|
|
import javafx.fxml.FXML;
|
|
import javafx.scene.control.Button;
|
|
import javafx.scene.control.ComboBox;
|
|
import javafx.scene.control.Label;
|
|
import javafx.scene.control.TextField;
|
|
|
|
public class DecimalController {
|
|
@FXML
|
|
private TextField inputField;
|
|
@FXML
|
|
private ComboBox<String> inputBaseComboBox; // 修改为inputBaseComboBox
|
|
@FXML
|
|
private ComboBox<String> outputBaseComboBox; // 添加输出进制下拉框
|
|
@FXML
|
|
private Label resultLabel;
|
|
|
|
// 选择输入进制的方法
|
|
@FXML
|
|
private void selectBase(ActionEvent event) {
|
|
// 该方法不再需要
|
|
}
|
|
|
|
// 转换方法
|
|
@FXML
|
|
private void convert() {
|
|
String input = inputField.getText();
|
|
String selectedInputBase = inputBaseComboBox.getValue(); // 获取输入进制
|
|
String selectedOutputBase = outputBaseComboBox.getValue(); // 获取输出进制
|
|
|
|
// 检查输入进制和输出进制是否已选择
|
|
if (selectedInputBase == null || selectedOutputBase == null) {
|
|
resultLabel.setText("请先选择输入和输出的进制!");
|
|
return;
|
|
}
|
|
|
|
int inputBase = Integer.parseInt(selectedInputBase); // 解析输入进制
|
|
int outputBase = Integer.parseInt(selectedOutputBase); // 解析输出进制
|
|
|
|
try {
|
|
// 将输入的数字转换为十进制
|
|
int decimalValue = Integer.parseInt(input, inputBase);
|
|
// 将十进制转换为目标进制
|
|
String result = Integer.toString(decimalValue, outputBase);
|
|
resultLabel.setText("转换结果: " + result);
|
|
} catch (NumberFormatException e) {
|
|
resultLabel.setText("输入的数字不符合选定的输入进制!");
|
|
} catch (Exception e) {
|
|
resultLabel.setText("转换过程中出现错误,请检查输入!");
|
|
}
|
|
}
|
|
}
|