Java gui,尝试转换为 gui 时出现客户端问题

Java gui, Client issue when trying to convert into gui

我在 java 中制作了一个 Client/Server 程序,我已经按照我的意愿使用 cmd 完美地工作了,现在我正在尝试将代码的客户端转换为 GUI ,但是我在打印客户端消息以及从文本字段和服务器消息中读取客户端输入时遇到了问题,这是我到目前为止所做的,编译时我没有收到任何错误,但它自己的 gui 没有 运行,任何帮助表示赞赏。 这是客户端代码:

import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;

    public class TcpClient
    {

        public static void main(String[] args)
        {

            try
            {
                new TcpClient().start();
            }

            catch(Exception e)
            {
                System.out.println("Major Error" + e.getMessage());
                e.printStackTrace();
            }

        }

        public void start() throws IOException
        {
            JFrame build = new JFrame("Client");
            JTextField serv = new JTextField();
            JTextField clie = new JTextField();
            build.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            serv.setBounds(50,210,300,50);
            build.add(serv);

            clie.setBounds(350,210,300,50);
            build.add(clie);
        //=====================================================================
            Socket clientSocket = null;
            InetAddress hostA = null;
            PrintWriter clientOutput = null;
            BufferedReader clientInput = null;
            BufferedReader standardInput = null;        
            try
            {

                hostA = InetAddress.getLocalHost();
                clientSocket = new Socket(hostA.getHostName(), 5600);
                clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                clientOutput = new PrintWriter(clientSocket.getOutputStream(), true);
                standardInput = new BufferedReader(new InputStreamReader(System.in));
                String serverMsg, clientMsg;

                //read from a socket and respond back to server
                while((serverMsg = clientInput.readLine()) != null)
                {
                    serv.setText("Server Saying - " + serverMsg);
                    if(serverMsg.equals("exit"))
                        break;

                    clientMsg = standardInput.readLine();
                    if(clientMsg != null)
                    {   
                        clie.setText("Client Saying - " + clientMsg);
                        clientOutput.println(clientMsg);
                    }
               }

         }

    catch(UnknownHostException e)
    {

        System.exit(1);
    }

    catch(IOException e)
    {

        System.exit(1);
    }   

    finally
    {
        //clean up time
        clientOutput.close();
        clientInput.close();
        standardInput.close();
        clientSocket.close();

    }       
        //=====================================================================
        build.setLayout(null);
        build.setSize(700,600);
        build.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        build.setVisible(true);
        build.setResizable(false);
        }
}

如评论中所述,您应该学习多线程,尤其是 EDT

现在发生的事情是您的代码和您的 GUI 相互阻碍,无法正常工作。通过在 EDT 上使用您的 GUI 运行,您的应用程序可以 运行 而无需阻止 GUI。当应用程序有与您的 GUI 相关的报告更改时,您可以在适当的时候通知 EDT。