-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatRoomPanel.java
More file actions
127 lines (105 loc) · 3.45 KB
/
ChatRoomPanel.java
File metadata and controls
127 lines (105 loc) · 3.45 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
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
// ChatRoomPanel.java
//
// Last modified 1/30/2000 by Alan Frindell
//
// GUI class for the chat room.
//
// You should not need to modify this class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChatRoomPanel extends JPanel {
JTextArea _inputArea;
JTextArea _outputArea;
JButton _quitButton;
ChatClient _client;
public ChatRoomPanel(ChatClient client) {
_client = client;
try {
componentInit();
} catch (Exception e) {
System.out.println("ChatRoomPanel error: " + e.getMessage());
e.printStackTrace();
}
}
void componentInit() throws Exception {
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridBag);
addLabel(gridBag, "Chat Room: ", SwingConstants.LEFT, 0, 0, 1, 1);
_outputArea = addArea(gridBag, new Dimension(400, 192), 0, 1);
_outputArea.setEditable(false);
addLabel(gridBag, "Your Message: ", SwingConstants.LEFT, 0, 2, 1, 1);
_inputArea = addArea(gridBag, new Dimension(400, 96), 0, 3);
_inputArea.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
inputKeyTyped(e);
}
});
_quitButton = new JButton("Leave Chat Room");
_quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
quit();
}
});
c.insets = new Insets(4, 4, 4, 4);
c.weighty = 1.0; //request any extra vertical space
c.gridx = 2;
c.gridy = 4;
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.SOUTHEAST; //bottom of space
gridBag.setConstraints(_quitButton, c);
add(_quitButton);
}
JLabel addLabel(GridBagLayout gridBag, String labelStr, int align,
int x, int y, int width, int height) {
GridBagConstraints c = new GridBagConstraints();
JLabel label = new JLabel(labelStr);
if (align == SwingConstants.LEFT) {
c.insets = new Insets(10, 4, 0, 4);
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = x;
c.gridy = y;
c.gridwidth = width;
c.gridheight = height;
gridBag.setConstraints(label, c);
add(label);
return label;
}
JTextArea addArea(GridBagLayout gridBag, Dimension prefSize,
int x, int y) {
JScrollPane scroller;
JTextArea area = new JTextArea();
GridBagConstraints c = new GridBagConstraints();
area.setLineWrap(true);
area.setWrapStyleWord(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(4, 4, 4, 4);
c.gridx = x;
c.gridy = y;
c.gridwidth = 3;
scroller = new JScrollPane(area);
scroller.setPreferredSize(prefSize);
gridBag.setConstraints(scroller, c);
add(scroller);
return area;
}
public JTextArea getOutputArea() {
return _outputArea;
}
void inputKeyTyped(KeyEvent e) {
String msg;
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
msg = _inputArea.getText();
_inputArea.setText("");
if (msg.length() == 0) {
return;
}
_client.sendMessage(msg);
}
}
void quit() {
_client.quit();
}
}