Browse Source

Merge branch 'main' of https://gitlab.cs.hs-fulda.de/fdai7332/java-chat

remotes/origin/server
Valentin Spiroski 11 months ago
parent
commit
a41db7eb2e
  1. 62
      src/main/java/ChatClient.java
  2. 5
      src/main/java/Constants.java

62
src/main/java/ChatClient.java

@ -2,24 +2,26 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.*;
import java.net.Socket; import java.net.Socket;
public class ChatClient extends JFrame implements KeyListener { public class ChatClient extends JFrame implements KeyListener {
private String address; private String address;
private int port;
private String connectionFailedMessage;
private Socket connectionToServer; private Socket connectionToServer;
private BufferedReader fromServerReader;
private PrintWriter toServerWriter;
// GUI // GUI
private JTextArea outputTextArea; private JTextArea outputTextArea;
private JTextField inputTextField; private JTextField inputTextField;
private JScrollPane outputScrollPane;
public ChatClient(int port) {
public ChatClient() {
super("Chat"); super("Chat");
this.port = port;
address = JOptionPane.showInputDialog("bitte IP-Adresse"); address = JOptionPane.showInputDialog("bitte IP-Adresse");
if (address != null) {
if (null != address) {
connectionFailedMessage = "Verbindung zum Server " + (address.isEmpty() ? "" : ("\"" + address + "\"")) + " fehlgeschlagen.";
receiveMessages(); receiveMessages();
} }
} }
@ -29,23 +31,56 @@ public class ChatClient extends JFrame implements KeyListener {
outputTextArea.setEditable(false); outputTextArea.setEditable(false);
outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat"));
outputScrollPane = new JScrollPane(outputTextArea);
inputTextField = new JTextField(); inputTextField = new JTextField();
inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben"));
inputTextField.addKeyListener(this); inputTextField.addKeyListener(this);
add(outputTextArea, BorderLayout.CENTER);
add(outputScrollPane, BorderLayout.CENTER);
add(inputTextField, BorderLayout.SOUTH); add(inputTextField, BorderLayout.SOUTH);
setVisible(true); setVisible(true);
setSize(800, 600);
setSize(Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
} }
private void receiveMessages() { private void receiveMessages() {
try { try {
connectionToServer = new Socket(address, port);
connectionToServer = new Socket(address, Constants.PORT);
fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream()));
toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream()));
initGui(); initGui();
} 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 (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();
}
} }
} }
@ -59,7 +94,8 @@ public class ChatClient extends JFrame implements KeyListener {
if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (e.getKeyCode() == KeyEvent.VK_ENTER) {
String message = inputTextField.getText(); String message = inputTextField.getText();
if (!message.isEmpty()) { if (!message.isEmpty()) {
// Senden die Nachricht an Server, wenn der erstellt wird
toServerWriter.println(message);
toServerWriter.flush();
inputTextField.setText(""); inputTextField.setText("");
} }
} }
@ -71,6 +107,6 @@ public class ChatClient extends JFrame implements KeyListener {
} }
public static void main(String[] args) { public static void main(String[] args) {
new ChatClient(3141);
new ChatClient();
} }
} }

5
src/main/java/Constants.java

@ -0,0 +1,5 @@
public class Constants {
public static final int WINDOW_WIDTH = 800;
public static final int WINDOW_HEIGHT = 600;
public static final int PORT = 3141;
}
Loading…
Cancel
Save