Browse Source

Merge branch 'feature/client/exchange-messages-between-client-server' into 'main'

Refactor code by using constants where possible

See merge request fdai7332/java-chat!5
remotes/origin/server
fdai5595 11 months ago
parent
commit
3ed649d906
  1. 22
      src/main/java/ChatClient.java
  2. 5
      src/main/java/Constants.java

22
src/main/java/ChatClient.java

@ -7,7 +7,7 @@ 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 BufferedReader fromServerReader;
private PrintWriter toServerWriter; private PrintWriter toServerWriter;
@ -17,12 +17,11 @@ public class ChatClient extends JFrame implements KeyListener {
private JTextField inputTextField; private JTextField inputTextField;
private JScrollPane outputScrollPane; 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();
} }
} }
@ -42,14 +41,15 @@ public class ChatClient extends JFrame implements KeyListener {
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);
fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream()));
toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream()));
connectionToServer = new Socket(address, Constants.PORT);
fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream()));
toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream()));
initGui(); initGui();
@ -59,7 +59,7 @@ public class ChatClient extends JFrame implements KeyListener {
outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum());
} }
} catch (IOException e) { } catch (IOException e) {
JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen.");
JOptionPane.showMessageDialog(null, connectionFailedMessage);
dispose(); dispose();
} finally { } finally {
if (null != connectionToServer) { if (null != connectionToServer) {
@ -107,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