|
|
@ -7,7 +7,7 @@ import java.net.Socket; |
|
|
|
|
|
|
|
public class ChatClient extends JFrame implements KeyListener { |
|
|
|
private String address; |
|
|
|
private int port; |
|
|
|
private String connectionFailedMessage; |
|
|
|
private Socket connectionToServer; |
|
|
|
private BufferedReader fromServerReader; |
|
|
|
private PrintWriter toServerWriter; |
|
|
@ -17,12 +17,11 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
private JTextField inputTextField; |
|
|
|
private JScrollPane outputScrollPane; |
|
|
|
|
|
|
|
public ChatClient(int port) { |
|
|
|
public ChatClient() { |
|
|
|
super("Chat"); |
|
|
|
this.port = port; |
|
|
|
address = JOptionPane.showInputDialog("bitte IP-Adresse"); |
|
|
|
|
|
|
|
if (address != null) { |
|
|
|
if (null != address) { |
|
|
|
connectionFailedMessage = "Verbindung zum Server " + (address.isEmpty() ? "" : ("\"" + address + "\"")) + " fehlgeschlagen."; |
|
|
|
receiveMessages(); |
|
|
|
} |
|
|
|
} |
|
|
@ -42,14 +41,15 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
add(inputTextField, BorderLayout.SOUTH); |
|
|
|
|
|
|
|
setVisible(true); |
|
|
|
setSize(800, 600); |
|
|
|
setSize(Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT); |
|
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE); |
|
|
|
setLocationRelativeTo(null); |
|
|
|
} |
|
|
|
private void receiveMessages() { |
|
|
|
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(); |
|
|
|
|
|
|
@ -59,7 +59,7 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); |
|
|
|
JOptionPane.showMessageDialog(null, connectionFailedMessage); |
|
|
|
dispose(); |
|
|
|
} finally { |
|
|
|
if (null != connectionToServer) { |
|
|
@ -107,6 +107,6 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
new ChatClient(3141); |
|
|
|
new ChatClient(); |
|
|
|
} |
|
|
|
} |