forked from mdetch/JChat
/
JChat.java
199 lines (165 loc) · 4.82 KB
/
JChat.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
188
189
190
191
192
193
194
195
196
197
198
199
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class JChat extends JFrame implements Runnable{
JButton openChat,chatWith,send;
JLabel pickPort,desPort;
JTextField pickText,desText,chatText;
JTextArea chatField;
PrintWriter out;
BufferedReader in;
public JChat(){
this.setSize(500, 600);
this.setResizable(false);
this.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(2,1));
//set up buttons
openChat = new JButton("Open to chat");
openChat.addActionListener(new OpenChat());
chatWith = new JButton("Chat with");
chatWith.addActionListener(new ChatWith());
send = new JButton("send");
send.addActionListener(new Send());
send.setEnabled(false);
InputMap inputMap = send.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
inputMap.put(enter, "ENTER");
send.getActionMap().put("ENTER", new ClickAction(send));
//set up labels
pickPort = new JLabel();
pickPort.setText("Pick your port number:");
desPort = new JLabel();
desPort.setText("Or enter a destinaltion port number:");
//set up text fields
pickText = new JTextField();
pickText.setPreferredSize(new Dimension(150,30));
desText = new JTextField();
desText.setPreferredSize(new Dimension(150,30));
chatText = new JTextField();
chatText.setPreferredSize(new Dimension(400,30));
chatText.setEnabled(false);
JPanel top1 = new JPanel();
top1.add(pickPort);
top1.add(pickText);
top1.add(openChat);
JPanel top2 = new JPanel();
top2.add(desPort);
top2.add(desText);
top2.add(chatWith);
topPanel.add(top1);
topPanel.add(top2);
chatField = new JTextArea();
chatField.setAutoscrolls(true);
chatField.setDragEnabled(true);
chatField.setEditable(false);
chatField.setAlignmentY(TOP_ALIGNMENT);
JPanel bottomPanel = new JPanel();
bottomPanel.add(chatText);
bottomPanel.add(send);
this.add(topPanel,BorderLayout.NORTH);
this.add(chatField,BorderLayout.CENTER);
this.add(bottomPanel,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enableChat(){
openChat.setEnabled(false);
chatWith.setEnabled(false);
pickText.setEnabled(false);
desText.setEnabled(false);
send.setEnabled(true);
chatText.setEnabled(true);
new Thread(this).start();
}
public static void main(String [] args){
JChat mainFrame = new JChat();
}
class OpenChat implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(pickText.getText().isEmpty()) return;
int port = Integer.parseInt(pickText.getText());
new Server(port);
}
}
class ChatWith implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(desText.getText().isEmpty()) return;
int port = Integer.parseInt(desText.getText());
new Client(port);
}
}
class Send implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String line = chatText.getText();
chatText.setText("");
out.println(line);
line = "\nYou says >> "+line;
chatField.append(line);
}
}
class Server{
private Socket clientSocket;
boolean serverContinue = true;
public Server(int port){
ServerSocket serverSocket = null;
try{
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
chatField.setText("------CONNECTION ESTABLISHED------");
out = new PrintWriter(clientSocket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
enableChat();
}catch(IOException e){
chatField.setText("Connection Fail");
}
}
}
class Client{
public Client(int port){
Socket socket = null;
try{
socket = new Socket("127.0.0.1",port);
chatField.setText("------CONNECTION ESTABLISHED------");
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
enableChat();
}catch(Exception e){
chatField.setText("Connection Failed");
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
String inputLine;
try {
while ((inputLine = in.readLine()) != null) {
inputLine = "\nYour peer says >> "+inputLine;
chatField.append(inputLine);
}
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class ClickAction extends AbstractAction {
private JButton button;
public ClickAction(JButton button) {
this.button = button;
}
public void actionPerformed(ActionEvent e) {
button.doClick();
}
}
}