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 inputBaseComboBox; // 修改为inputBaseComboBox @FXML private ComboBox 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("转换过程中出现错误,请检查输入!"); } } }