Browse Source

Merge branch 'hotfix/client/improve-chat-window-appearence'

remotes/origin/hotfix/client/improve-chat-window-appearence
Alena Bandarovich 11 months ago
parent
commit
db3242af7e
  1. 27
      src/main/java/ChatClient.java
  2. 7
      src/main/java/ClientHandler.java

27
src/main/java/ChatClient.java

@ -1,10 +1,14 @@
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ChatClient extends JFrame implements KeyListener { public class ChatClient extends JFrame implements KeyListener {
public static class TR extends JFrame { public static class TR extends JFrame {
@ -70,6 +74,8 @@ public class ChatClient extends JFrame implements KeyListener {
private JTextArea outputTextArea; private JTextArea outputTextArea;
private JTextField inputTextField; private JTextField inputTextField;
private JScrollPane outputScrollPane; private JScrollPane outputScrollPane;
private Timer timer;
private JLabel timeLabel;
public ChatClient() { public ChatClient() {
super("Chat"); super("Chat");
@ -80,6 +86,22 @@ public class ChatClient extends JFrame implements KeyListener {
} }
} }
private void initTimer() {
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateTime();
}
});
timer.start();
}
private void updateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String currentTime = sdf.format(new Date());
timeLabel.setText("Current Time: " + currentTime);
}
private void initGui() { private void initGui() {
outputTextArea = new JTextArea(); outputTextArea = new JTextArea();
outputTextArea.setEditable(false); outputTextArea.setEditable(false);
@ -88,6 +110,10 @@ public class ChatClient extends JFrame implements KeyListener {
outputScrollPane = new JScrollPane(outputTextArea); outputScrollPane = new JScrollPane(outputTextArea);
timeLabel = new JLabel();
updateTime();
add(timeLabel, BorderLayout.NORTH);
inputTextField = new JTextField(); inputTextField = new JTextField();
inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben"));
inputTextField.addKeyListener(this); inputTextField.addKeyListener(this);
@ -107,6 +133,7 @@ public class ChatClient extends JFrame implements KeyListener {
toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream()));
initGui(); initGui();
initTimer();
while (true) { while (true) {
String message = fromServerReader.readLine(); String message = fromServerReader.readLine();

7
src/main/java/ClientHandler.java

@ -1,8 +1,7 @@
import javax.swing.*;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import javax.swing.JOptionPane;
public class ClientHandler implements Runnable { public class ClientHandler implements Runnable {
private ChatServer chatServer; private ChatServer chatServer;
private Socket connectionToClient; private Socket connectionToClient;
@ -15,7 +14,9 @@ public class ClientHandler implements Runnable {
public ClientHandler(ChatServer chatServer, Socket connectionToClient) { public ClientHandler(ChatServer chatServer, Socket connectionToClient) {
this.chatServer = chatServer; this.chatServer = chatServer;
this.connectionToClient = connectionToClient; this.connectionToClient = connectionToClient;
name = JOptionPane.showInputDialog("Benutzername für neuen Client vergeben");
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
name = JOptionPane.showInputDialog(dialog,"Benutzername für neuen Client vergeben");
new Thread(this).start();} // Start a new thread for this client handler new Thread(this).start();} // Start a new thread for this client handler

Loading…
Cancel
Save