【JAVA快速编写UI】 Java 编写一个编码转换和加解密工具,可以创建一个简单的 GUI 应用程序(例子)
EncodingDecodingTool/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── rockmelodies/
│ │ │ └── encodingdecodingtool/
│ │ │ ├── MainApp.java
│ │ │ ├── controller/
│ │ │ │ └── MainController.java
│ │ │ └── util/
│ │ │ └── CryptoUtils.java
│ │ └── resources/
│ │ └── com/
│ │ └── rockmelodies/
│ │ └── encodingdecodingtool/
│ │ └── view/
│ │ ├── main_layout.fxml
│ │ └── styles.css
└── pom.xml
# MainApp.java
package com.rockmelodies.encodingdecodingtool;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/com/rockmelodies/encodingdecodingtool/view/main_layout.fxml"));
primaryStage.setTitle("Encoding & Decoding Tool");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
# CryptoUtils.java
package com.rockmelodies.encodingdecodingtool;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/com/rockmelodies/encodingdecodingtool/view/main_layout.fxml"));
primaryStage.setTitle("Encoding & Decoding Tool");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
# MainController
package com.rockmelodies.encodingdecodingtool.controller;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import com.rockmelodies.encodingdecodingtool.util.CryptoUtils;
public class MainController {
@FXML
private TextField inputField;
@FXML
private TextArea outputArea;
@FXML
private void handleBase64Encode() {
String input = inputField.getText();
String encoded = CryptoUtils.base64Encode(input);
outputArea.setText(encoded);
}
@FXML
private void handleBase64Decode() {
String input = inputField.getText();
String decoded = CryptoUtils.base64Decode(input);
outputArea.setText(decoded);
}
public void switchTheme(String theme) {
Scene scene = inputField.getScene();
scene.getStylesheets().clear();
scene.getStylesheets().add(getClass().getResource("/com/rockmelodies/encodingdecodingtool/view/" + theme + ".css").toExternalForm());
}
// Add more handlers for other encoding/decoding and encryption/decryption methods
}
main_layout.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.rockmelodies.encodingdecodingtool.controller.MainController">
<TextField fx:id="inputField" promptText="Enter text here..."/>
<Button text="Base64 Encode" onAction="#handleBase64Encode"/>
<Button text="Base64 Decode" onAction="#handleBase64Decode"/>
<!-- Add more buttons for other encoding/decoding and encryption/decryption methods -->
<TextArea fx:id="outputArea" editable="false" promptText="Output will appear here..."/>
</VBox>
styless.css
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.rockmelodies.encodingdecodingtool.controller.MainController">
<TextField fx:id="inputField" promptText="Enter text here..."/>
<Button text="Base64 Encode" onAction="#handleBase64Encode"/>
<Button text="Base64 Decode" onAction="#handleBase64Decode"/>
<!-- Add more buttons for other encoding/decoding and encryption/decryption methods -->
<TextArea fx:id="outputArea" editable="false" promptText="Output will appear here..."/>
</VBox>