-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatFrame.java
More file actions
62 lines (53 loc) · 1.99 KB
/
ChatFrame.java
File metadata and controls
62 lines (53 loc) · 1.99 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
/**
* ChatFrame is the GUI of server and client.
*
*/
public class ChatFrame extends JFrame {
private String userName;
private JTextArea history;
private JTextField msgToBeSent;
/**
* Construct a ServerFrame.
*/
public ChatFrame(String userName, String serverHost, int port) {
this.userName = userName;
JPanel infoPanel = new JPanel();
infoPanel.add(new JLabel(String.format("Server: %s Port: %s User: %s", serverHost, port, userName )));
add(infoPanel, BorderLayout.NORTH);
JPanel chatPanel = initChatPanel(userName);
add(chatPanel, BorderLayout.CENTER);
pack();
}
private JPanel initChatPanel(final String nickname){
JPanel chatPanel = new JPanel();
chatPanel.setLayout(new BorderLayout());
JPanel msgPanel = new JPanel();
msgToBeSent = new JTextField(50);
msgToBeSent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
String msg = String.format("%tT %s: %s", new Date(), nickname, msgToBeSent.getText());
ChatClient.sendMsg(msg);
msgToBeSent.setText("");
}
});
msgPanel.add(msgToBeSent);
chatPanel.add(msgPanel, BorderLayout.NORTH);
JPanel hisPanel = new JPanel();
history = new JTextArea(20, 50);
JScrollPane scrollPane = new JScrollPane(history);
scrollPane.setEnabled(false);
Border titled = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "chatting history");
hisPanel.setBorder(titled);
hisPanel.add(scrollPane);
chatPanel.add(hisPanel, BorderLayout.CENTER);
return chatPanel;
}
public void showMessage(String msg) {
history.append(msg+"\n");
}
}