---
title: "javase-富文本聊天工具"
author: "Function"
date: "2012-02-25T15:22:52Z"
tags: []
categories: ["开源软件"]
slug: "javase"
draft: false
lastmod: "2012-02-25T15:22:52Z"
id: 1423675
url: "https://milker.iteye.com/blog/1423675"
---

学完Java基础，也来写个聊天的工具，给它起了个名：GG聊天（只是希望沾点光）；
功能实现如下：
（1）动态请求服务器，登录会记住用户名的
（2）好友列表显示（在线的为彩色，离线的为灰色）
（3）聊天的字体可更改（富文本编辑），查看聊天记录，
（4）注册
（5）查找用户
（6）添加在线用户（别人添加你，可以拒绝的哦）
（7）DAO模块有MySql改为XML，只要启动服务器，不需要数据库也可玩玩看;
------
带几张截图，有兴趣的可以下载下去：
**登录框（点击设置，可以设置服务器IP和端口）：**![](http://dl.iteye.com/upload/attachment/0063/7522/273d25b4-5b8c-3fef-8d12-afe6e5ac8a68.jpg)
**注册：**
![](http://dl.iteye.com/upload/attachment/0063/7525/9e2dfbd1-b76c-3809-8771-fa171ae4aca0.jpg)
**注册是系统自动分配一个可用数字（看完要记住de，别忘了）：**
![](http://dl.iteye.com/upload/attachment/0063/7527/0a88cc08-b881-3272-a720-4ce42aa69609.jpg)
**下面是登录后的UI（还没人上线的..）：**![](http://dl.iteye.com/upload/attachment/0063/7529/8a70b328-be98-3066-acc8-396f2c9b015c.jpg)
**这是聊天的窗口（可以设置字体--还可以发送抖动窗口的）：**
![](http://dl.iteye.com/upload/attachment/0063/7537/5fcdfed7-4d57-3ec2-a85b-d5930180a326.jpg)
**查看聊天的记录：**![](http://dl.iteye.com/upload/attachment/0063/7531/11bcead9-a433-32f5-99df-e476d2ca9f13.jpg)
**好友上线啦：**![](http://dl.iteye.com/upload/attachment/0063/7533/3e395203-56a1-34af-bfd0-5ed2655b3a5f.jpg)
**可以抖他的聊天窗口的：**![](http://dl.iteye.com/upload/attachment/0063/7535/77e61dfe-3290-3b26-85f5-7327aaf4d8b6.jpg)
**查找好友的：**
![](http://dl.iteye.com/upload/attachment/0063/7539/17606686-d3d8-3966-b729-0a1f751a8597.jpg)
---------------------------------
下面，奉上核心代码：
服务器的控制中心（没有采用ChannelSocket,用户太少啦，另一个版本使用那个的，有兴趣的我可以把那部分代码发给你。）

```java
try {
			while (start) {
				final Message message = (Message) ois.readObject();
				new Thread() {
					@Override
					public void run() {
						String action = message.getAction();
						ServerUI.setMessage("@server: " + action);
						try {
							ServicesCache.get(
									Configuration.getClassName(action))
									.execute(ServiceThread.this, oos, message);
						} catch (Exception e1) {
							System.err.println("catch error");
							try {
								Class<?> ac = Configuration.getClazz(action);
								ac.getMethod("execute",
												ServiceThread.class,
												ObjectOutputStream.class,
												Message.class).invoke(
												ac.newInstance(),
												ServiceThread.this, oos,
												message);
							} catch (Exception e) {
								start = false;
								e.printStackTrace();
							}
						}
					}
				}.start();
			}
		} catch (Exception e) {
			start = false;
			ServerUI.setMessage("用户断开连接！");
		} finally {

		}
```


------------------
控制中心的标准：

```java
public abstract class ActionSupport {

	private final UserDao dao = new UserDaoXMLImpl();

	protected final UserDao getDao() {
		return dao;
	}

	public abstract String execute(ServiceThread service, ObjectOutputStream oos,
			Message message) throws Exception;
}
```

------------------
**哦，我忘了，还有一个可爱的系统托盘：**

![](http://dl.iteye.com/upload/attachment/0063/7541/2ff4e1b3-df8e-3a64-9723-17221caee206.jpg)

**在附上服务器的UI：**
![](http://dl.iteye.com/upload/attachment/0063/7543/604a2a0e-ed6b-34b3-978b-7af024395a6d.jpg)

下面就是源码啦，无私奉献