我正在使用套接字和文件i / o构建基于GUI的聊天应用程序。 我对这个程序的过程的想法是:
当我键入任何消息并点击输入时,消息不会转到服务器 - >客户,反之亦然。该消息显示在同一GUI的chatWindow
(即JTextArea
)中,这很奇怪。这样做的正确方法是什么?我想阅读&连接到网络后写入流。
GUI的行为如下:
这里是实例变量:
JPanel contentPane;
JButton send;
DataInputStream dis;
DataOutputStream dos;
ServerSocket server;
Socket conn;
JLabel attach = new JLabel("");
JTextField t1 = new JTextField();
JTextArea chatWindow = new JTextArea();
这是我的主要内容:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChatPage2 frame = new ChatPage2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
我已将actionListener事件添加到文本字段,并尝试将文本从textfield附加到名为chatWindow的textarea:
t1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String line = t1.getText();
chatWindow.append(line + "\n");
if ((evt.getSource() == line) && (t1.getText() != "")) {
chatWindow.setText(chatWindow.getText() + '\n' + "ME:"
+ t1.getText());
try {
DataOutputStream dos = new DataOutputStream(
conn.getOutputStream());
dos.writeUTF(t1.getText());
} catch (Exception e1) {
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
t1.setText("");
}
}
});
对于服务器连接,下面是我申请的代码:
// connect是事件按钮...当点击连接按钮时,网络将连接。
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
server=new ServerSocket(2020, 10, InetAddress.getLocalHost());
chatWindow.setText("Waiting for Client");
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
new Thread(()-> {
try {
conn=server.accept();
chatWindow.setText(chatWindow.getText() + '\n' + "Client Found");
//chatWindow.setText(chatWindow.getText() + '\n' + "Client Found");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
try {
DataInputStream dis = new DataInputStream(conn.getInputStream());
String string = dis.readUTF();
//string = br.readLine();
chatWindow.setText(chatWindow.getText() + '\n' + "Client:"
+ string);
} catch (IOException ioException) {
chatWindow.setText(chatWindow.getText() + '\n'
+ "Sorry, Cant send the message !");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
});
对于客户端连接,我在代码下面应用了:
// connect是事件按钮...当点击连接按钮时,网络将连接。
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Thread(()-> {
try {
conn = new Socket(InetAddress.getLocalHost(),2020);
chatWindow.setText("Connected to SERVER");
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
try {
DataInputStream dis = new DataInputStream(conn.getInputStream());
String string = dis.readUTF();
//string = br.readLine();
chatWindow.setText(chatWindow.getText() + '\n' + "Server:"
+ string);
} catch (IOException ioException) {
chatWindow.setText(chatWindow.getText() + '\n'
+ "Sorry, Cant send the message !");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
});