From aa10ff56c616beea88cd391093074c29f38d411e Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 17:24:02 +0100 Subject: [PATCH 1/7] Create connection to server using socket --- src/main/java/ChatClient.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index c4f2737..3627c91 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,12 +1,31 @@ import javax.swing.*; +import java.io.IOException; +import java.net.Socket; public class ChatClient { private String address; - public ChatClient() { + private int port; + private Socket connectionToServer; + + public ChatClient(int port) { + this.port = port; address = JOptionPane.showInputDialog("bitte IP-Adresse"); + + if (address != null) { + receiveMessages(); + } + } + + private void receiveMessages() { + try { + connectionToServer = new Socket(address, port); + } catch (IOException e) { + JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + } } public static void main(String[] args) { - new ChatClient(); + new ChatClient(3141); + } } \ No newline at end of file From 3072cf2853cdc552f781ecfefb53ec787b588a4b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 19:51:03 +0100 Subject: [PATCH 2/7] add GUI window for chat --- src/main/java/ChatClient.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 3627c91..5c60638 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -2,12 +2,13 @@ import javax.swing.*; import java.io.IOException; import java.net.Socket; -public class ChatClient { +public class ChatClient extends JFrame { private String address; private int port; private Socket connectionToServer; - + public ChatClient(int port) { + super("Chat"); this.port = port; address = JOptionPane.showInputDialog("bitte IP-Adresse"); @@ -16,10 +17,18 @@ public class ChatClient { } } + private void initGui() { + setVisible(true); + setSize(800, 600); + setDefaultCloseOperation(EXIT_ON_CLOSE); + } private void receiveMessages() { try { - connectionToServer = new Socket(address, port); - } catch (IOException e) { + // Um GUI zu testen + // Kommentar in der Zeile 28 wird weggenohmen, wenn der Server erstellt wird. + // connectionToServer = new Socket(address, port); + initGui(); + } catch (Exception e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } } From 1e3e1ffe4a7ede432bb5411e0818e77dbafd7ba8 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 20:23:33 +0100 Subject: [PATCH 3/7] add input and output areas in GUI window --- src/main/java/ChatClient.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 5c60638..00600d0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -7,6 +7,10 @@ public class ChatClient extends JFrame { private int port; private Socket connectionToServer; + // GUI + private JTextArea outputTextArea; + private JTextField inputTextField; + public ChatClient(int port) { super("Chat"); this.port = port; @@ -18,6 +22,12 @@ public class ChatClient extends JFrame { } private void initGui() { + outputTextArea = new JTextArea(); + inputTextField = new JTextField(); + + add(outputTextArea); + add(inputTextField); + setVisible(true); setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); From 6d32cd126c5df9b2ca9d4b3047a93516cb55eb48 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 21:11:38 +0100 Subject: [PATCH 4/7] Change position of input and output areas --- src/main/java/ChatClient.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 00600d0..6380f66 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,4 +1,5 @@ import javax.swing.*; +import java.awt.*; import java.io.IOException; import java.net.Socket; @@ -23,10 +24,13 @@ public class ChatClient extends JFrame { private void initGui() { outputTextArea = new JTextArea(); + outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); + inputTextField = new JTextField(); + inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); - add(outputTextArea); - add(inputTextField); + add(outputTextArea, BorderLayout.CENTER); + add(inputTextField, BorderLayout.SOUTH); setVisible(true); setSize(800, 600); From 92244c512b47796f38eab6fed55491b818c0a1a6 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 21:14:19 +0100 Subject: [PATCH 5/7] Prevent writing in output area --- src/main/java/ChatClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 6380f66..8f0c1f0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -24,6 +24,7 @@ public class ChatClient extends JFrame { private void initGui() { outputTextArea = new JTextArea(); + outputTextArea.setEditable(false); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); inputTextField = new JTextField(); From 3800aeb4618cd70b201820622e4496dbf8c0fd46 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 21:54:23 +0100 Subject: [PATCH 6/7] Remove text from input field and later send to server after user presses enter --- src/main/java/ChatClient.java | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) 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 +} From 3f4a302efcc17102265600cf5369c20af4fed0a5 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Mon, 5 Feb 2024 21:19:44 +0100 Subject: [PATCH 7/7] Establish connection to server --- src/main/java/ChatClient.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 695f622..8d0b776 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -42,9 +42,7 @@ public class ChatClient extends JFrame implements KeyListener { } private void receiveMessages() { try { - // Um GUI zu testen - // Kommentar in der Zeile 28 wird weggenohmen, wenn der Server erstellt wird. - // connectionToServer = new Socket(address, port); + connectionToServer = new Socket(address, port); initGui(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen.");