parent
ac94c06c7d
commit
c3b94fd678
@ -0,0 +1,29 @@
|
|||||||
|
import javafx.application.Application;
|
||||||
|
import javafx.fxml.FXMLLoader;
|
||||||
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.layout.GridPane;
|
||||||
|
import javafx.stage.Stage;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class BaseConverterApp extends Application {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Stage primaryStage) {
|
||||||
|
try {
|
||||||
|
// 加载FXML文件
|
||||||
|
FXMLLoader loader = new FXMLLoader(getClass().getResource("BaseConverter.fxml"));
|
||||||
|
GridPane root = loader.load();
|
||||||
|
// 设置场景和舞台
|
||||||
|
Scene scene = new Scene(root, 400, 250);
|
||||||
|
primaryStage.setTitle("进制转换器");
|
||||||
|
primaryStage.setScene(scene);
|
||||||
|
primaryStage.show();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
launch(args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
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(); // 转换为目标进制
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue