我正在使用套接字开发消息传递应用程序。我有一个初始JFrame
,允许用户选择要连接的联系人,并让他们选择是否要成为客户端或服务器。当他们做出这个选择时,它会调用一个创建聊天窗口框架的方法,并使用SwingWorker
初始化连接,在聊天窗口的发送按钮上创建一个动作列表器,以便在发送消息时发送消息按下,并创建一个在窗口打开时始终在监听的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();
}
network
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;
}
}
chatWindowScreen
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);
}
}