|
|
@ -2,17 +2,20 @@ import javax.swing.*; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.event.KeyEvent; |
|
|
|
import java.awt.event.KeyListener; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.*; |
|
|
|
import java.net.Socket; |
|
|
|
|
|
|
|
public class ChatClient extends JFrame implements KeyListener { |
|
|
|
private String address; |
|
|
|
private int port; |
|
|
|
private Socket connectionToServer; |
|
|
|
private BufferedReader fromServerReader; |
|
|
|
private PrintWriter toServerWriter; |
|
|
|
|
|
|
|
// GUI |
|
|
|
private JTextArea outputTextArea; |
|
|
|
private JTextField inputTextField; |
|
|
|
private JScrollPane outputScrollPane; |
|
|
|
|
|
|
|
public ChatClient(int port) { |
|
|
|
super("Chat"); |
|
|
@ -29,11 +32,13 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
outputTextArea.setEditable(false); |
|
|
|
outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); |
|
|
|
|
|
|
|
outputScrollPane = new JScrollPane(outputTextArea); |
|
|
|
|
|
|
|
inputTextField = new JTextField(); |
|
|
|
inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); |
|
|
|
inputTextField.addKeyListener(this); |
|
|
|
|
|
|
|
add(outputTextArea, BorderLayout.CENTER); |
|
|
|
add(outputScrollPane, BorderLayout.CENTER); |
|
|
|
add(inputTextField, BorderLayout.SOUTH); |
|
|
|
|
|
|
|
setVisible(true); |
|
|
@ -43,9 +48,39 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
private void receiveMessages() { |
|
|
|
try { |
|
|
|
connectionToServer = new Socket(address, port); |
|
|
|
fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); |
|
|
|
toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); |
|
|
|
|
|
|
|
initGui(); |
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
while (true) { |
|
|
|
String message = fromServerReader.readLine(); |
|
|
|
outputTextArea.append(message + "\n"); |
|
|
|
outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); |
|
|
|
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) { |
|
|
|
String message = inputTextField.getText(); |
|
|
|
if (!message.isEmpty()) { |
|
|
|
// Senden die Nachricht an Server, wenn der erstellt wird |
|
|
|
toServerWriter.println(message); |
|
|
|
toServerWriter.flush(); |
|
|
|
inputTextField.setText(""); |
|
|
|
} |
|
|
|
} |
|
|
|