parent
1819462a36
commit
47f26e0216
@ -0,0 +1,54 @@
|
||||
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("转换过程中出现错误,请检查输入!");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package java6239.uml;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class DecimalConversion extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
// 加载 FXML 文件
|
||||
Parent root = FXMLLoader.load(getClass().getResource("DecimalConversion.fxml"));
|
||||
|
||||
// 设置场景
|
||||
Scene scene = new Scene(root,400,400);
|
||||
|
||||
// 设置窗口标题
|
||||
primaryStage.setTitle("进制转换器");
|
||||
|
||||
// 设置场景并显示窗口
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue