DatagramSocket UDP 服务器 Swing InvokeLater 不工作

DatagramSocket UDP Server Swing InvokeLater not working

尝试制作 DatagramSocket UDP server/client 聊天应用程序,但在接收到带有 DatagramPacket 的数据包后更新 GUI 时遇到一些问题。这是我的代码;我唯一遗漏的,我认为是无关紧要的,是我的 JFrame window.

的组布局代码
public class Server extends JFrame {

    JButton startServerButton;
    JTextField textField;
    JTextArea textArea;

    public Server() {

        super("Server");
        initComponents();
        setResizable(false);
        setVisible(true);

    }

    private void startServer() throws IOException {

        DatagramSocket serverSocket;
        String sentence;

        startServerButton.setText("Server started");
        startServerButton.setEnabled(false);

        while (true) {

            serverSocket = new DatagramSocket(9876);

            // receive
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            sentence = new String(receivePacket.getData());
            int port = receivePacket.getPort();
            System.out.println("RECEIVED (port " + port + "): " + sentence);
            showMessage("RECEIVED (port " + port + "): " + sentence);
            InetAddress IPAddress = receivePacket.getAddress();

            // send
            byte[] sendData = sentence.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);
            serverSocket.close();

        }

    }

    private void showMessage(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                textArea.append("\n" + text);
            }
        });
    }

    private void initComponents() {

        startServerButton = new JButton();
        textField = new JTextField();
        textArea = new JTextArea();
        textArea.setEditable(false);



        startServerButton.setText("Start server");
        startServerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                try {
                    startServer();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        ...grouplayout stuff here...


    }

    public static void main(String args[]) {
        Server server = new Server();
    }

}

所以问题是,我的 JFrame 上的 textArea 永远不会更新,但我的 System.out.println() 总是正确打印从我的客户端 class 收到的消息。我可以为我的客户 class 显示代码,但我觉得它无关紧要。如果有任何帮助,我将不胜感激。

您必须在另一个线程中启动您的套接字侦听器。否则你会阻止重绘。

像这样:

startServerButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    startServer();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }
});