Browse Source

neue ChatGUI(noch nicht eingebunden

remotes/origin/server
Paul Kattenborn 11 months ago
parent
commit
62d3eb5578
  1. 52
      src/main/java/ChatGUI.java
  2. 1
      src/main/java/ClientHandler.java
  3. 44
      src/test/java/ChatServerTest.java

52
src/main/java/ChatGUI.java

@ -0,0 +1,52 @@
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatGUI implements ActionListener {
JTextField inputTextField = new JTextField();
JTextArea outputTextArea = new JTextArea();
JFrame gui;
public ChatGUI() {
gui = new JFrame();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("java-chat");
gui.setLayout(new BorderLayout());
JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
gui.add(outputScrollPane, BorderLayout.CENTER);
inputTextField.setPreferredSize(new Dimension(0, 30));
gui.add(inputTextField, BorderLayout.SOUTH);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int quarterWidth = screenSize.width / 2;
int quarterHeight = screenSize.height / 2;
gui.setSize(quarterWidth, quarterHeight);
gui.setVisible(true);
}
public static void main(String[] args) {
new ChatGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}

1
src/main/java/ClientHandler.java

@ -66,6 +66,7 @@ public class ClientHandler implements Runnable {
toClientWriter.println(message); // Send the message to the client toClientWriter.println(message); // Send the message to the client
toClientWriter.flush(); // Flush the stream toClientWriter.flush(); // Flush the stream
} }
//Test method
public String getLastMessage() { public String getLastMessage() {
return lastMessage; return lastMessage;
} }

44
src/test/java/ChatServerTest.java

@ -1,44 +0,0 @@
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import org.junit.jupiter.api.Test;
class ChatServerTest {
@Test
public void testConstructor() {
// Arrange
ChatServer chatServer = null;
// Act
try {
chatServer = new ChatServer(1234);
} catch (Exception e) {
fail("Exception occurred: " + e.getMessage());
}
// Assert
assertNotNull(chatServer);
}
@Test
public void testBroadcastMessage() {
// Arrange
ChatServer chatServer = new ChatServer(1234);
ClientHandler client1 = new ClientHandler(chatServer, null);
ClientHandler client2 = new ClientHandler(chatServer, null);
chatServer.getClients().add(client1);
chatServer.getClients().add(client2);
// Act
chatServer.broadcastMessage("Test message");
// Assert
List<ClientHandler> clients = chatServer.getClients();
for (ClientHandler client : clients) {
assertEquals("Test message", client.getLastMessage());
}
}
}
Loading…
Cancel
Save