-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuiChat.java
188 lines (158 loc) · 7.3 KB
/
GuiChat.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package remote;
//====== GUI Packages ======
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.SpringLayout;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import java.awt.Color;
import javax.swing.SwingConstants;
//====== Handling events ======
import java.awt.event.*;
//====== Mapping (GuiChat - Actor) ======
import akka.actor.ActorRef;
public class GuiChat extends JFrame {
private JPanel contentPane;
private JTextField username;
private JLabel lblOnlineUsers;
private JButton btnLogin;
private JButton btnDisconnect;
private JScrollPane scrollPane;
private JTextArea room;
private JScrollPane scrollPane_1;
private JTextArea usersList;
private JScrollPane scrollPane_2;
private JTextArea clientInput;
private JButton btnSend;
private ActorRef communicator;
private Messages messages;
public JTextArea getRoom () {
return this.room;
}
public void setActorReference(ActorRef communicator) {
this.communicator = communicator;
}
public GuiChat() {
messages = new Messages();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 629, 642);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
SpringLayout sl_contentPane = new SpringLayout();
contentPane.setLayout(sl_contentPane);
JLabel lblUsername = new JLabel("Username");
sl_contentPane.putConstraint(SpringLayout.NORTH, lblUsername, 15, SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.WEST, lblUsername, 10, SpringLayout.WEST, contentPane);
lblUsername.setFont(new Font("Yu Gothic UI Semilight", Font.PLAIN, 15));
contentPane.add(lblUsername);
username = new JTextField("");
username.setHorizontalAlignment(SwingConstants.CENTER);
username.setFont(new Font("Yu Gothic UI Semilight", Font.PLAIN, 12));
sl_contentPane.putConstraint(SpringLayout.EAST, lblUsername, -5, SpringLayout.WEST, username);
sl_contentPane.putConstraint(SpringLayout.NORTH, username, 2, SpringLayout.NORTH, lblUsername);
sl_contentPane.putConstraint(SpringLayout.WEST, username, 97, SpringLayout.WEST, contentPane);
username.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnLogin.setEnabled(false);
username.setEnabled(false);
clientInput.setEnabled(true);
btnSend.setEnabled(true);
communicator.tell(messages.new LoginMessage(username.getText()),null);
}
});
contentPane.add(username);
username.setColumns(10);
btnDisconnect = new JButton("Disconnect");
btnDisconnect.setForeground(new Color(255, 69, 0));
sl_contentPane.putConstraint(SpringLayout.NORTH, btnDisconnect, 12, SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.WEST, btnDisconnect, 332, SpringLayout.WEST, contentPane);
btnDisconnect.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 14));
btnDisconnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clientInput.setEnabled(false);
btnSend.setEnabled(false);
}
});
contentPane.add(btnDisconnect);
lblOnlineUsers = new JLabel("Online Users");
sl_contentPane.putConstraint(SpringLayout.EAST, lblOnlineUsers, -30, SpringLayout.EAST, contentPane);
lblOnlineUsers.setForeground(new Color(0, 0, 0));
lblOnlineUsers.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 14));
contentPane.add(lblOnlineUsers);
btnLogin = new JButton("Sign in");
btnLogin.setForeground(new Color(50, 205, 50));
sl_contentPane.putConstraint(SpringLayout.EAST, username, -6, SpringLayout.WEST, btnLogin);
sl_contentPane.putConstraint(SpringLayout.NORTH, btnLogin, 12, SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.EAST, btnLogin, -6, SpringLayout.WEST, btnDisconnect);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clientInput.setEnabled(true);
btnSend.setEnabled(true);
}
});
btnLogin.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 14));
contentPane.add(btnLogin);
scrollPane = new JScrollPane();
sl_contentPane.putConstraint(SpringLayout.NORTH, scrollPane, 19, SpringLayout.SOUTH, btnDisconnect);
sl_contentPane.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, contentPane);
sl_contentPane.putConstraint(SpringLayout.SOUTH, scrollPane, -96, SpringLayout.SOUTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.EAST, scrollPane, -146, SpringLayout.EAST, contentPane);
contentPane.add(scrollPane);
room = new JTextArea("");
room.setEditable(false);
room.setFont(new Font("Yu Gothic UI Semilight", Font.PLAIN, 15));
scrollPane.setViewportView(room);
scrollPane_1 = new JScrollPane();
sl_contentPane.putConstraint(SpringLayout.SOUTH, lblOnlineUsers, -1, SpringLayout.NORTH, scrollPane_1);
sl_contentPane.putConstraint(SpringLayout.NORTH, scrollPane_1, 0, SpringLayout.NORTH, scrollPane);
sl_contentPane.putConstraint(SpringLayout.SOUTH, scrollPane_1, -5, SpringLayout.SOUTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.WEST, scrollPane_1, 6, SpringLayout.EAST, scrollPane);
sl_contentPane.putConstraint(SpringLayout.EAST, scrollPane_1, -10, SpringLayout.EAST, contentPane);
contentPane.add(scrollPane_1);
usersList = new JTextArea("");
usersList.setEditable(false);
usersList.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 13));
scrollPane_1.setViewportView(usersList);
scrollPane_2 = new JScrollPane();
sl_contentPane.putConstraint(SpringLayout.NORTH, scrollPane_2, 6, SpringLayout.SOUTH, scrollPane);
sl_contentPane.putConstraint(SpringLayout.WEST, scrollPane_2, 10, SpringLayout.WEST, contentPane);
sl_contentPane.putConstraint(SpringLayout.SOUTH, scrollPane_2, 0, SpringLayout.SOUTH, scrollPane_1);
sl_contentPane.putConstraint(SpringLayout.EAST, scrollPane_2, -227, SpringLayout.EAST, contentPane);
contentPane.add(scrollPane_2);
clientInput = new JTextArea("");
clientInput.setFont(new Font("Yu Gothic UI Semilight", Font.PLAIN, 14));
scrollPane_2.setViewportView(clientInput);
//textAreaInput
clientInput.setEnabled(false);
btnSend = new JButton("Send");
sl_contentPane.putConstraint(SpringLayout.WEST, btnSend, 6, SpringLayout.EAST, scrollPane_2);
sl_contentPane.putConstraint(SpringLayout.SOUTH, btnSend, 0, SpringLayout.SOUTH, scrollPane_1);
btnSend.setForeground(new Color(50, 205, 50));
btnSend.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 13));
btnSend.setEnabled(false);
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
communicator.tell( messages.new ChatMessage(clientInput.getText()) , null);
clientInput.setText("");
}
});
sl_contentPane.putConstraint(SpringLayout.NORTH, btnSend, 6, SpringLayout.SOUTH, scrollPane);
sl_contentPane.putConstraint(SpringLayout.EAST, btnSend, 0, SpringLayout.EAST, scrollPane);
contentPane.add(btnSend);
}
}