diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 8f0c1f0..695f622 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,9 +1,11 @@ import javax.swing.*; import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; import java.io.IOException; import java.net.Socket; -public class ChatClient extends JFrame { +public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; @@ -29,6 +31,7 @@ public class ChatClient extends JFrame { inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); + inputTextField.addKeyListener(this); add(outputTextArea, BorderLayout.CENTER); 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) { new ChatClient(3141); - } -} \ No newline at end of file +}