-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClientGUI.java
More file actions
94 lines (76 loc) · 3.38 KB
/
ChatClientGUI.java
File metadata and controls
94 lines (76 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.function.Consumer;
/*
Author - The-R34per
Last Updated October 20th, 2025
ChatClientGUI.java © 2025 by The-R34per
Licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International.
To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/
*/
public class ChatClientGUI extends JFrame {
private final JTextArea chatArea = new JTextArea(20, 50);
private final JTextField inputField = new JTextField(40);
private ChatClient client;
private String displayName;
public ChatClientGUI() {
setTitle("Encrypted Chat (AES-GCM)");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chatArea.setEditable(false);
JScrollPane scroll = new JScrollPane(chatArea);
JPanel bottom = new JPanel();
bottom.add(inputField);
JButton sendBtn = new JButton("Send");
bottom.add(sendBtn);
getContentPane().add(scroll, BorderLayout.CENTER);
getContentPane().add(bottom, BorderLayout.SOUTH);
sendBtn.addActionListener(e -> sendInput());
inputField.addActionListener(e -> sendInput());
pack();
setLocationRelativeTo(null);
}
private void sendInput() {
String text = inputField.getText().trim();
if (text.isEmpty() || client == null) return;
try {
client.sendPlain(displayName + ": " + text);
inputField.setText("");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Send failed: " + ex.getMessage());
}
}
private void startClient(String host, int port, String name, char[] pass) {
this.displayName = name;
Consumer<String> onMessage = (msg) -> SwingUtilities.invokeLater(() -> {
chatArea.append(msg + "\n");
chatArea.setCaretPosition(chatArea.getDocument().getLength());
});
client = new ChatClient(host, port, pass, onMessage);
try {
client.start(name);
chatArea.append("Connected to " + host + ":" + port + "\n");
chatArea.append("Tip: use the same password on all clients to decrypt messages.\n");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Could not start client: " + ex.getMessage());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
ChatClientGUI gui = new ChatClientGUI();
String host = JOptionPane.showInputDialog(gui, "Server host:", "localhost");
String portStr = JOptionPane.showInputDialog(gui, "Server port:", "4444");
String name = JOptionPane.showInputDialog(gui, "Display name:", "Anonymous");
JPasswordField pf = new JPasswordField();
int ok = JOptionPane.showConfirmDialog(gui, pf, "Enter Password:", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (ok != JOptionPane.OK_OPTION) {
System.exit(0);
}
char[] pass = pf.getPassword();
int port = 4444;
try { port = Integer.parseInt(portStr); } catch (Exception ignored) {}
gui.setVisible(true);
gui.startClient(host, port, name, pass);
});
}
}