Browse Source

Remove text from input field and later send to server after user presses enter

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

27
src/main/java/ChatClient.java

@ -1,9 +1,11 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
public class ChatClient extends JFrame {
public class ChatClient extends JFrame implements KeyListener {
private String address; private String address;
private int port; private int port;
private Socket connectionToServer; private Socket connectionToServer;
@ -29,6 +31,7 @@ public class ChatClient extends JFrame {
inputTextField = new JTextField(); inputTextField = new JTextField();
inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben"));
inputTextField.addKeyListener(this);
add(outputTextArea, BorderLayout.CENTER); add(outputTextArea, BorderLayout.CENTER);
add(inputTextField, BorderLayout.SOUTH); add(inputTextField, BorderLayout.SOUTH);
@ -48,8 +51,28 @@ public class ChatClient extends JFrame {
} }
} }
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
String message = inputTextField.getText();
if (!message.isEmpty()) {
// Senden die Nachricht an Server, wenn der erstellt wird
inputTextField.setText("");
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) { public static void main(String[] args) {
new ChatClient(3141); new ChatClient(3141);
} }
} }
Loading…
Cancel
Save