Java 关闭和重新打开使用SwingWorker发送和接收消息的JFrame时出现问题

Java 关闭和重新打开使用SwingWorker发送和接收消息的JFrame时出现问题,java,swing,sockets,swingworker,Java,Swing,Sockets,Swingworker,我正在开发一个带有套接字的消息传递应用程序。我有一个初始的JFrame,允许用户选择要连接的联系人,并让他们选择是作为客户机还是服务器。当他们做出此选择时,它会调用一种方法,在该方法中创建聊天窗口框架,并使用SwingWorker初始化连接,在聊天窗口的send buton上创建一个action listner,以便按下时发送消息,并创建一个SocketListener,在窗口打开时始终侦听 这工作得很好,但是如果我关闭聊天窗口,然后再次选择服务器或客户端重新打开它,什么都不会发生-框架加载了,

我正在开发一个带有套接字的消息传递应用程序。我有一个初始的
JFrame
,允许用户选择要连接的联系人,并让他们选择是作为客户机还是服务器。当他们做出此选择时,它会调用一种方法,在该方法中创建聊天窗口框架,并使用
SwingWorker
初始化连接,在聊天窗口的send buton上创建一个action listner,以便按下时发送消息,并创建一个
SocketListener
,在窗口打开时始终侦听

这工作得很好,但是如果我关闭聊天窗口,然后再次选择服务器或客户端重新打开它,什么都不会发生-框架加载了,但是我无法在上面发送任何消息

我不确定这里的问题是什么,可能是我没有正确处理秋千工人或框架吗?任何帮助都将不胜感激,并为我缺乏知识而道歉

以下是此的相关代码:

public static void server() {
    JButton server = new JButton();
    server.setText("Server");

    server.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            String name = nameChoice;
            String ip = ipChoice;
            String port = portChoice;

            try {
                server2(port);
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    panel.add(server);
}

public static void server2(String port) throws Exception {
    gui.frame();

    SwingWorker worker = new SwingWorker() {
        @Override
        protected Void doInBackground() throws Exception {
            sockets.serverSetup(port);
            gui.sendButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String msg = gui.newMsg.getText();
                    gui.newMsg.setText(null);
                    try {
                        sockets.sendMsg(msg);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
            while(gui.open==true) {
                sockets.receiveMsg();
            }
            sockets.closeConnection();
            System.out.println("Closed");
            return null;
        }
    };
    worker.execute();
}
网络
1) 为了更快地获得更好的帮助,请发布一个or。2) 请学习常见的Java命名法(命名约定-例如
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是
大写常量
),并一致使用它。3) 在源代码中只需要一行空白就可以了。
{
之后或
}
之前的空行通常也是多余的。1)为了更快地获得更好的帮助,请发布或。2) 请学习常见的Java命名法(命名约定-例如
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是
大写常量
),并一致使用它。3) 在源代码中只需要一行空白就可以了。
{
之后或
}
之前的空行通常也是多余的。
    package com.company;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;

public class network {

    static ServerSocket s;
    static Socket s1;
    static OutputStream s1out;
    static DataOutputStream dos;
    static InputStream s1in;

    static chatWindowScreen gui = new chatWindowScreen();

    static String st;

    public static Vector sentVec = new Vector();
    public static Vector receivedVec = new Vector();

    public static Vector messages = new Vector();

    public static void serverSetup(String port) throws IOException {
        System.out.println("Test");
        int portInt = Integer.parseInt(port);
        s = new ServerSocket(portInt);
        s1 = s.accept();
        System.out.println("Server set up");
        //sendMsg("Hi");
    }

    public static void clientSetup(String port, String ip) throws IOException {
        System.out.println("Test");
        int portInt = Integer.parseInt(port);
        s1 = new Socket(ip, portInt);
        System.out.println("Client set up");
        //receiveMsg();
    }

    public static void closeConnection() throws IOException {
        dos.close();
        s1out.close();
        s1.close();
    }

    public static void sendMsg(String message) throws IOException {
        System.out.println("Test");

        s1out = s1.getOutputStream();
        dos = new DataOutputStream(s1out);
        dos.writeUTF(message);

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String t1 = sdf.format(cal.getTime());

        message = t1 + " - " + message;

        sentVec.add(message);
        receivedVec.add("");

        messages.add(message);

        gui.sent.setText("Sent \n");

        for (int i = 0; i < sentVec.size(); i++) {
            gui.sent.append(sentVec.get(i) + "\n");
        }

        gui.received.setText("Received \n");

        for (int i = 0; i < receivedVec.size(); i++) {
            gui.received.append(receivedVec.get(i) + "\n");
        }
        System.out.println("complete");
    }

    public static void receiveMsg() throws IOException {

        System.out.println("Test");

        s1in = s1.getInputStream();
        DataInputStream dis = new DataInputStream(s1in);
        st = (dis.readUTF());

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String t1 = sdf.format(cal.getTime());
        st = t1 + " - " + st;

        receivedVec.add(st);
        sentVec.add("");

        messages.add(st);

        gui.received.setText("Received \n");

        for (int i = 0; i < receivedVec.size(); i++) {
            gui.received.append((String) receivedVec.get(i) + "\n");
        }

        gui.sent.setText("Sent \n");

        for (int i = 0; i < sentVec.size(); i++) {

            gui.sent.append((String) sentVec.get(i) + "\n");
        }
    }

    public static Vector getSentVec() {
        return sentVec;
    }

    public static Vector getReceivedVec() {
        return receivedVec;

    }
}
package com.company;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.Vector;

public class chatWindowScreen {

    static JFrame frame = new JFrame();
    static JPanel panel = new JPanel();
    static JTextArea sent;
    static JTextArea received;
    static JTextArea newMsg;
    static JButton sendButton;
    public static String msg;
    static boolean open = true;

    static network sockets = new network();

    public static boolean close = false;

    public void frame() throws Exception {

        System.out.println("GUI opened");

        frame.setTitle("Messaging Application");
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
            }

            @Override
            public void windowClosing(WindowEvent e) {
            }

            @Override
            public void windowClosed(WindowEvent e) {

                System.out.println("Closed");
                panel.removeAll();
                open=false;
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowActivated(WindowEvent e) {
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }
        });

        sentMsg();
        receivedMsg();
        msgBox();
        sendBtn();
        save();

        frame.add(panel);
        frame.setVisible(true);
    }

    public static void sentMsg() {
        sent = new JTextArea(15, 20);
        sent.setText("Sent" + "\n");
        sent.setLineWrap(true);
        sent.setEditable(false);
        JScrollPane pane = new JScrollPane(sent, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(pane);
    }

    public static void receivedMsg() {
        received = new JTextArea(15, 20);
        received.setText("Received" + "\n");
        received.setEditable(false);
        received.setLineWrap(true);
        JScrollPane pane1 = new JScrollPane(received, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(pane1);
    }

    public static void msgBox() {
        newMsg = new JTextArea(1, 15);
        newMsg.setText("");
        newMsg.setLineWrap(true);
        JScrollPane pane2 = new JScrollPane(newMsg, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(pane2);
    }

    public static void sendBtn() {

        sendButton = new JButton();
        sendButton.setText("Send!");

        /*sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                msg = newMsg.getText();
                newMsg.setText(null);

                SwingWorker worker = new SwingWorker() {
                    @Override
                    protected Object doInBackground() throws Exception {
                        sockets.sendMsg(msg);
                        return null;
                    }
                };
                worker.execute();

                try {
                    sockets.sendMsg(msg);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                msg = null;

            }
        });*/

        panel.add(sendButton);
    }

    public static void save(){

        JButton save = new JButton();
        save.setText("Save Conversation");
        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveToFile save = new saveToFile();
            }
        });

        panel.add(save);
    }
}