From 02c45605a48dea910dc235a7130c2a9e08194675 Mon Sep 17 00:00:00 2001 From: Marc Dimmerling Date: Fri, 9 Feb 2024 16:13:24 +0100 Subject: [PATCH] ChatClient-Fix --- src/main/java/ChatClient.java | 67 ++++++++++++++++------------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 8476ae0..30b486c 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -16,8 +16,8 @@ public class ChatClient extends JFrame implements KeyListener { private JTextArea outputTextArea; private JTextField inputTextField; private JScrollPane outputScrollPane; - - public ChatClient(int port) { + + public ChatClient() { super("Chat"); address = JOptionPane.showInputDialog("bitte IP-Adresse"); if (null != address) { @@ -30,8 +30,6 @@ public class ChatClient extends JFrame implements KeyListener { outputTextArea = new JTextArea(); outputTextArea.setEditable(false); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); - - outputScrollPane = new JScrollPane(outputTextArea); outputScrollPane = new JScrollPane(outputTextArea); @@ -54,40 +52,35 @@ public class ChatClient extends JFrame implements KeyListener { toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); initGui(); - - // while (true) { - // String message = fromServerReader.readLine(); - // outputTextArea.append(message + "\n"); - // outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); - // } - // Solang fromServerReader nicht erstellt ist als Kommentar - } - - - - catch (Exception e) { - JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + + while (true) { + String message = fromServerReader.readLine(); + outputTextArea.append(message + "\n"); + outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); + } + } catch (IOException e) { + JOptionPane.showMessageDialog(null, connectionFailedMessage); dispose(); - } - finally { - if (connectionToServer != null) { - try { - connectionToServer.close(); - } - - catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - /* if (fromServerReader != null) { - - fromServerReader.close(); (warten auf ServerReader) - } */ - /* if (toServerWriter != null) { - - toServerWriter.close(); (warten auf ServerWriter) - } */ + } finally { + if (null != connectionToServer) { + try { + connectionToServer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + if (null != fromServerReader) { + try { + fromServerReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + if(null != toServerWriter) { + toServerWriter.close(); + } } }