Compare commits
No commits in common. 'main' and 'master' have entirely different histories.
@ -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,73 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.geometry.Insets?>
|
||||||
|
<?import javafx.scene.control.Button?>
|
||||||
|
<?import javafx.scene.control.ComboBox?>
|
||||||
|
<?import javafx.scene.control.Label?>
|
||||||
|
<?import javafx.scene.control.TextField?>
|
||||||
|
<?import javafx.scene.layout.BorderPane?>
|
||||||
|
<?import javafx.scene.layout.GridPane?>
|
||||||
|
<?import javafx.collections.FXCollections?>
|
||||||
|
|
||||||
|
<?import java.lang.String?>
|
||||||
|
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="java6239.uml.DecimalController">
|
||||||
|
<center>
|
||||||
|
<GridPane hgap="10" vgap="10" alignment="center" style="-fx-padding: 20px;">
|
||||||
|
<!-- 第一行:选择输入进制 -->
|
||||||
|
<Label text="请选择输入数据的进制类型:" GridPane.rowIndex="0" GridPane.columnSpan="6" />
|
||||||
|
<ComboBox fx:id="inputBaseComboBox" GridPane.columnSpan="6" GridPane.rowIndex="1">
|
||||||
|
<items>
|
||||||
|
<FXCollections fx:factory="observableArrayList">
|
||||||
|
<String fx:value="2" />
|
||||||
|
<String fx:value="3" />
|
||||||
|
<String fx:value="4" />
|
||||||
|
<String fx:value="5" />
|
||||||
|
<String fx:value="6" />
|
||||||
|
<String fx:value="7" />
|
||||||
|
<String fx:value="8" />
|
||||||
|
<String fx:value="9" />
|
||||||
|
<String fx:value="10" />
|
||||||
|
<String fx:value="11" />
|
||||||
|
<String fx:value="12" />
|
||||||
|
<String fx:value="13" />
|
||||||
|
<String fx:value="14" />
|
||||||
|
<String fx:value="15" />
|
||||||
|
<String fx:value="16" />
|
||||||
|
</FXCollections>
|
||||||
|
</items>
|
||||||
|
</ComboBox>
|
||||||
|
|
||||||
|
<!-- 第二行:输入数据 -->
|
||||||
|
<Label text="请输入数据:" GridPane.rowIndex="2" />
|
||||||
|
<TextField fx:id="inputField" GridPane.columnSpan="6" GridPane.rowIndex="2" />
|
||||||
|
|
||||||
|
<!-- 第三行:转换成进制和转换按钮 -->
|
||||||
|
<Label text="转换成进制:" GridPane.rowIndex="3" />
|
||||||
|
<ComboBox fx:id="outputBaseComboBox" GridPane.columnIndex="1" GridPane.rowIndex="3">
|
||||||
|
<items>
|
||||||
|
<FXCollections fx:factory="observableArrayList">
|
||||||
|
<String fx:value="2" />
|
||||||
|
<String fx:value="3" />
|
||||||
|
<String fx:value="4" />
|
||||||
|
<String fx:value="5" />
|
||||||
|
<String fx:value="6" />
|
||||||
|
<String fx:value="7" />
|
||||||
|
<String fx:value="8" />
|
||||||
|
<String fx:value="9" />
|
||||||
|
<String fx:value="10" />
|
||||||
|
<String fx:value="11" />
|
||||||
|
<String fx:value="12" />
|
||||||
|
<String fx:value="13" />
|
||||||
|
<String fx:value="14" />
|
||||||
|
<String fx:value="15" />
|
||||||
|
<String fx:value="16" />
|
||||||
|
</FXCollections>
|
||||||
|
</items>
|
||||||
|
</ComboBox>
|
||||||
|
<Button text="转换" onAction="#convert" GridPane.rowIndex="3" GridPane.columnIndex="2" />
|
||||||
|
|
||||||
|
<!-- 第四行:输出结果 -->
|
||||||
|
<Label fx:id="resultLabel" text="" GridPane.rowIndex="4" GridPane.columnSpan="6" />
|
||||||
|
</GridPane>
|
||||||
|
</center>
|
||||||
|
</BorderPane>
|
@ -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);
|
||||||
|
|
||||||
|
// 设置窗口标题
|
||||||
|
primaryStage.setTitle("进制转换器");
|
||||||
|
|
||||||
|
// 设置场景并显示窗口
|
||||||
|
primaryStage.setScene(scene);
|
||||||
|
primaryStage.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
launch(args);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue