图5-3 登录界面图
客户端好友列表界面如图5-4所示:
图5-4 好友列表界面
21
登录模块关键代码如下:
public void login()
{
String name=textField用户名.getText();
String password=new String(pwd密码.getPassword()); if(name.equals(\
JOptionPane.showMessageDialog(this, \用户名和密码不能为空!\错误
\ERROR_MESSAGE);
else {
try {
if(cs_TCP==null)
cs_TCP=new CS_TCP(MyTools.QQServerIP,
MyTools.QQServerPort,this,main);
System.out.println(\开始检测用户名和密码……\
cs_TCP.sendMessage(Flag.LOGIN+MyTools.FLAGEND+name+MyTools.SPLIT1+password+MyTools.SPLIT1+main.getServerPort()+MyTools.SPLIT1+comboBox状态.getSelectedIndex());
}
}
}
catch (Exception e) { }
JOptionPane.showMessageDialog(null, \连接服务器失败!请检查网络连接或确
保QQ服务器已启动!\
5.1.3 聊天模块
聊天模块是即时聊天系统的主要功能模块,本系统开发的目的就是实现即时聊天。在好友列表中单击鼠标选中一位好友后双击鼠标,即可打开聊天界面(如图5-5)。在界面下方的消息输入框内输入消息内容,点击发送按钮,即将消息发送给对方。点击表情按钮,还可以发送类似QQ表情的消息给对方。
聊天界面如图5-5所示:
22
图5-5 聊天界面
聊天模块关键代码如下:
(1)鼠标事件,完成鼠标进入好友List中,选中好友,双击与好友进行聊天。 private class ListMouseAdapter extends MouseAdapter{
public void mouseMoved(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {
if(e.getSource()==listFriend ){
if(e.getClickCount()==2){
23
//System.out.println(e.getSource()); if(e.getSource()==listFriend){ }
listFriend.clearSelection();
int index = listFriend.locationToIndex(e.getPoint()); //System.out.println(\listFriend.setSelectedIndex(index);
}
}
}
}
chatWithFriend();
if(e.getButton()==MouseEvent.BUTTON3){ } return;
popupMenu.show(listFriend, e.getX(), e.getY());
if(e.getSource()==btnMenu){ }
if(e.getSource()==btnSys){ }
broadcastwindow.showNow();
menu.show(btnMenu, e.getX()-25, e.getY()-75);
(2)打开好友聊天窗口。 private void chatWithFriend(){
Object obj = listFriend.getSelectedValue(); if(obj instanceof FriendUser){ } } package data;
import java.io.Serializable; import java.util.Date;
24
FriendUser friendUser = (FriendUser)obj; int jqnum = friendUser.getJqnum(); ChatPane chatPane = chat.get(jqnum); if(chatPane==null){ }
else if(chatPane.isDisplayable()){ } else{ }
chatPane.setVisible(true); chatPane.setFocusable(true); chatPane.setState(NORMAL);
chatPane = new ChatPane(oos,friendUser,selfUser,true); chat.put(jqnum, chatPane);
5.1.4 后台管理模块
作为用户我们只是使用了即时聊天系统的客户端功能,即我们电脑上安装的即时聊天软件。其实为了保证软件的正常运行,软件运行商在各地都有自己的代理服务器,以提供足够的带宽和计算能力。在软件的身后,自然会有强大的后台支撑,这保证了我们用户能够享受到即时聊天软件高质量的服务。本系统模拟了一个简单地后台管理的功能,主要提供了系统服务和用户管理两个功能。 1.系统服务功能
在系统服务里面,管理员可以启动和关闭服务器,实际上就是启动服务器端一些线程,开始监听来自客户端登录的请求,并随时做出相应的处理。同时查询数据库,返回用户信息。
具体功能实现如图5-6所示:
图5-6 服务器端系统服务功能
关键代码如下:
/**
* 启动服务器。
* @throws IOException IO异常。
25