Browse Source

Send message to server when user presses enter

remotes/origin/feature/client/exchange-messages-between-client-server
Alena Bandarovich 11 months ago
parent
commit
ced5aae13b
  1. 9
      src/main/java/ChatClient.java

9
src/main/java/ChatClient.java

@ -2,13 +2,14 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.*;
import java.net.Socket;
public class ChatClient extends JFrame implements KeyListener {
private String address;
private int port;
private Socket connectionToServer;
private PrintWriter toServerWriter;
// GUI
private JTextArea outputTextArea;
@ -43,8 +44,9 @@ public class ChatClient extends JFrame implements KeyListener {
private void receiveMessages() {
try {
connectionToServer = new Socket(address, port);
toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream()));
initGui();
} catch (Exception e) {
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen.");
}
}
@ -59,7 +61,8 @@ public class ChatClient extends JFrame implements KeyListener {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
String message = inputTextField.getText();
if (!message.isEmpty()) {
// Senden die Nachricht an Server, wenn der erstellt wird
toServerWriter.println(message);
toServerWriter.flush();
inputTextField.setText("");
}
}

Loading…
Cancel
Save