FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Head First Java/SimpleChatClient.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Head First Java
/
SimpleChatClient.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (72 loc) · 2.75 KB
Breadcrumbs
JavaExercises
/
Head First Java
/
SimpleChatClient.java
Copy path
File metadata and controls
83 lines (72 loc) · 2.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
io
.*;
import
java
.
net
.*;
//import java.util.*;
import
javax
.
swing
.*;
import
java
.
awt
.*;
import
java
.
awt
.
event
.*;
public
class
SimpleChatClient
{
JTextArea
incoming
;
JTextField
outgoing
;
BufferedReader
reader
;
PrintWriter
writer
;
Socket
sock
;
public
static
void
main
(
String
[]
args
) {
SimpleChatClient
client
=
new
SimpleChatClient
();
client
.
go
();
}
public
void
go
() {
JFrame
frame
=
new
JFrame
(
"Simple Chat Client"
);
frame
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
JPanel
mainPanel
=
new
JPanel
();
incoming
=
new
JTextArea
(
25
,
40
);
incoming
.
setLineWrap
(
true
);
incoming
.
setWrapStyleWord
(
true
);
incoming
.
setEditable
(
false
);
JScrollPane
qScroller
=
new
JScrollPane
(
incoming
);
qScroller
.
setVerticalScrollBarPolicy
(
ScrollPaneConstants
.
VERTICAL_SCROLLBAR_ALWAYS
);
qScroller
.
setHorizontalScrollBarPolicy
(
ScrollPaneConstants
.
HORIZONTAL_SCROLLBAR_NEVER
);
outgoing
=
new
JTextField
(
33
);
outgoing
.
addActionListener
(
new
SendButtonListener
());
JButton
sendButton
=
new
JButton
(
"Send"
);
sendButton
.
addActionListener
(
new
SendButtonListener
());
mainPanel
.
add
(
qScroller
);
mainPanel
.
add
(
outgoing
);
mainPanel
.
add
(
sendButton
);
setUpNetworking
();
Thread
readerThread
=
new
Thread
(
new
IncomingReader
());
readerThread
.
start
();
frame
.
getContentPane
().
add
(
BorderLayout
.
CENTER
,
mainPanel
);
frame
.
setBounds
(
200
,
200
,
470
,
475
);
frame
.
setVisible
(
true
);
}
private
void
setUpNetworking
() {
try
{
sock
=
new
Socket
(
"127.0.0.1"
,
5000
);
InputStreamReader
streamReader
=
new
InputStreamReader
(
sock
.
getInputStream
());
reader
=
new
BufferedReader
(
streamReader
);
writer
=
new
PrintWriter
(
sock
.
getOutputStream
());
System
.
out
.
println
(
"Networking established."
);
}
catch
(
Exception
ex
) {
ex
.
printStackTrace
(); }
}
public
class
SendButtonListener
implements
ActionListener
{
public
void
actionPerformed
(
ActionEvent
ev
) {
try
{
writer
.
println
(
outgoing
.
getText
());
writer
.
flush
();
}
catch
(
Exception
ex
) {
ex
.
printStackTrace
(); }
outgoing
.
setText
(
""
);
outgoing
.
requestFocus
();
}
}
public
class
IncomingReader
implements
Runnable
{
public
void
run
() {
String
message
;
try
{
while
((
message
=
reader
.
readLine
()) !=
null
) {
System
.
out
.
println
(
"read "
+
message
);
incoming
.
append
(
message
+
"
\n
"
);
}
}
catch
(
Exception
ex
) {
ex
.
printStackTrace
(); }
}
}
}
Back
|
FazBrowse Home
|
New Git URL