客户端需要实现的功能主要是登录、发送信息和即时显示聊天信息。界面也是对象比较简单,大家需要建立两个窗口界面:1、登录界面和聊天界面,界面比较简单,不做讲解了。
一、封装功能类
我们先把客户端端所需要用到的功能封装起来,在com.dao包中新建Client类,实现代码如下:
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
|
/**
* 客户端
*
* @author Administrator
*
*/
public class Client {
private Socket socket;
private PrintWriter out;
private BufferedReader in;
// 连接 》》 登陆
public boolean connect(String host, int port) {
try {
socket = new Socket(host, port);
out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(
socket.getOutputStream())));
in = new BufferedReader( new InputStreamReader(
socket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
return false ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false ;
}
return true ;
}
// 关闭连接 》》 退出
public void close() {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 发送信息
*
* @param message
*/
public void send(String message) {
out.println(message);
out.flush();
}
/**
* 接收信息
*
* @return
*/
public String receive() throws Exception{
String message = null ;
message = in.readLine();
return message;
}
}
|
二、登录功能
绑定登录按钮的单击事件,首先获得用户的输入,然后调用上面封装类的connect方法,连接成功后把用户名发送个服务器端,关闭当前窗口,启动聊天窗口,聊天界面和登录界面所使用的客户端对象clinet是同一个,所以在启动聊天界面时要把客户端对象clinet传给聊天界面。实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//1、获得用户的输入
String host=jTextField1.getText();
int port=Integer.parseInt(jTextField2.getText());
String name=jTextField3.getText();
//2、登陆
Client clinet= new Client();
boolean result=clinet.connect(host, port);
if (result){
clinet.send(name); //发送服务器
this .dispose();
FrameUtil.showCenterFrame( new ChatFrame(clinet,name));
} else {
JOptionPane.showMessageDialog( this , "连接出错" , "出错了" ,JOptionPane.ERROR_MESSAGE);
}
}
|
三、发送信息
绑定聊天界面的发送按钮的单击事件,实现代码如下:
1
2
3
4
5
6
7
8
9
10
|
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//发送信息给服务器
//1、获得用户输入的信息
String message = messagejTextField.getText();
//2、发送
client.send(message);
//
messagejTextField.setText( "" );
}
|
四、监听服务器发来的聊天信息。
我们需要在聊天界面上面新建一个线程,不断的监听服务器有没有新的聊天信息发送,如果有,就把聊天信息显示在界面上。修改聊天窗口类的构造方法。在构造方法中启动一个线程,实现代码如下:
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
|
private Client client;
private String name;
/** Creates new form ChatFrame */
public ChatFrame( final Client client, String name) {
this .client = client;
this .name = name;
initComponents();
userjLabel.setText(name);
//启动一个线程不断监听服务器发来的信息,并显示
new Thread() {
public void run() {
try {
while ( true ) {
String message = client.receive();
chatjTextArea.setText(chatjTextArea.getText() + message
+ "\r\n" );
}
} catch (Exception e) {
//JOptionPane.showMessageDialog(this, "")
//e.printStackTrace();
}
}
}.start();
}
|
这样我们的系统就开发完毕了,当然还有一些优化功能还需要不断完善,比如窗口关闭后要绑定关闭事件,把socket也关闭等等。这里就不详细说明,具体大家可以下载源码查看。