【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>

在这里插入图片描述

在这里插入图片描述

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一些代码示例,来帮助您实现这个功能。以下是一个使用Java编写简单的客户端/服务器通信程序。 服务器代码: ```java import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws IOException { // 创建一个ServerSocket对象来监听客户端的连接请求 ServerSocket serverSocket = new ServerSocket(1234); System.out.println("服务器已启动,等待客户端连接..."); // 等待客户端连接 Socket clientSocket = serverSocket.accept(); System.out.println("客户端已连接,IP地址为:" + clientSocket.getInetAddress().getHostAddress()); // 获取输入输出流 BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); // 读取客户端发送的消息并回复 String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("客户端说:" + inputLine); out.println("服务器已收到您的消息:" + inputLine); } // 关闭连接 out.close(); in.close(); clientSocket.close(); serverSocket.close(); } } ``` 客户端代码: ```java import java.net.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Client extends JFrame implements ActionListener { private JTextArea messageArea; private JTextField messageField; private Socket clientSocket; private BufferedReader in; private PrintWriter out; public Client() { super("客户端"); // 创建UI界面 messageArea = new JTextArea(10, 30); messageArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(messageArea); messageField = new JTextField(30); messageField.addActionListener(this); JButton sendButton = new JButton("发送"); sendButton.addActionListener(this); JPanel panel = new JPanel(); panel.add(scrollPane); panel.add(messageField); panel.add(sendButton); add(panel); // 连接服务器 try { clientSocket = new Socket("localhost", 1234); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); out = new PrintWriter(clientSocket.getOutputStream(), true); messageArea.append("连接服务器成功!\n"); } catch (IOException e) { messageArea.append("连接服务器失败!\n"); } // 启动UI界面 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } public void actionPerformed(ActionEvent evt) { String message = messageField.getText(); out.println(message); messageField.setText(""); } public void run() { // 读取服务器的消息并在UI界面上显示 String response; try { while ((response = in.readLine()) != null) { messageArea.append(response + "\n"); } } catch (IOException e) { messageArea.append("与服务器的连接已断开!\n"); } // 关闭连接 out.close(); try { in.close(); clientSocket.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Client client = new Client(); client.run(); } } ``` 这段代码实现了一个简单的客户端/服务器通信功能,并且使用了Java Swing库来创建了一个简单UI界面。您可以根据您的需求对代码进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rockmelodies

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值