JAVA课程设计.docx
《JAVA课程设计.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计.docx(11页珍藏版)》请在冰豆网上搜索。
JAVA课程设计
JAVA课程设计报告
——聊天工具
姓名:
专业班级:
10级计科一班
系(院):
计算机科学学院
1.课程设计目的
本次课程设计的目的在于通过实践加深学生对面向对象程序设计的理论、方法和基础知识的理解,掌握使用Java语言进行面向对象设计的基本方法,提高运用面向对象知识分析实际问题、解决实际问题的能力。
2.功能设计与实现
(1)服务器端程序:
负责发送用户列表和转发客户端发送来的信息
(2)客户端程序:
可连接到服务器,并将消息发送到服务器端和接受服务器端发送来的信息。
(3)其它内容:
面向对象技术中的继承与多态(重载和覆盖)机制、各种修饰符的使用
类、包、接口的定义与使用
a.常用工具类与算法的实现(数组、向量、字符串、链表)
b.Java常用标准GUI组件及其事件处理
c.Java的异常处理机制
(4)连接:
登陆界面输入完整之后将用户的信息发送到服务器端,服务器端创建相应的连接,并将更新后的用户列表发送给所有用户。
a.刷新列表:
使客户端发送一段指令到服务器端,然后由服务器端广播新的用户列表到所有的客户端。
b.发送:
根据发送者、发送对象、发送的话创建一个字段发送到服务器端
c.接收:
根据从服务器端接收到的信息判断发送者、接受者、以及要听的话,选择显示在公共聊天区还是私人聊天区。
d.颜色:
设置字体颜色。
e.字体:
设置字体,在楷体和宋体之间切换。
f.主题:
设置背景图片。
g.快捷键:
设置是否开启shift+enter发送快捷键。
3.概要设计
本程序根据功能设置了两个界面:
登陆界面以及聊天界面。
(1)登陆界面:
如下图
(2)聊天界面:
如下图
4.主要源代码
(1)登陆界面连接服务器:
publicvoidlink()throwsException{//连接服务器核心代码
hostName=jTextField2.getText().trim();//主机地址
port=Integer.parseInt(jTextField3.getText());//端口号,默认是4331,参考教科书
client=newSocket(hostName,port);//主机地址和端口号组成套接字
in=newBufferedReader(newInputStreamReader(client.getInputStream()));//从服务器接收到的
out=newPrintWriter(client.getOutputStream());//发送出去的
out.println(jTextField1.getText()+"&"+sex);//类似这种格式123&Boy
out.flush();//刷新.输出缓冲区
}
(2)聊天界面发送信息:
publicvoidenter()
{
Stringmywords,outmsg;
StringwithWho=(String)jComboBox1.getSelectedItem();//获取是和谁说话
try{
mywords=jTextArea4.getText();//我说的话
if((mywords.trim()).length()!
=0){//不能发送空消息也不能都发空格
outmsg="withWho&"+name+"&"+withWho+"&"+mywords;//类似withWho&tom&hello
out.println(outmsg);
out.flush();
if(withWho.equals("所有人")){
jTextArea2.append(name+"说:
"+mywords+"\n");
}
else{//对某个人交谈
jTextArea2.append(name+"对"+withWho+"说:
"+mywords+"\n");
}
}
}catch(Exceptionee){
System.out.println(ee);
jTextArea2.append("与服务器连接中断,请重新登陆!
\n");
}
finally{
jTextArea4.setText("");
}
}
(3)聊天界面接收信息代码:
publicvoidrun(){
Stringinmsg;
while(true){//循环
try{
inmsg=in.readLine();//从流中输入
System.out.println("inmsg"+inmsg);
jList1.setModel(model1);//model和jList参考网上
String[]userInfo=inmsg.split("&");
if(inmsg.startsWith("new")){//新人
jTextArea1.append("欢迎"+userInfo[1]+"\n");
model1.addElement(userInfo[1]+"〖"+userInfo[2]+"〗");
}
elseif(inmsg.startsWith("old")){
model1.addElement(userInfo[1]+"〖"+userInfo[2]+"〗");//更新用户列表
}
//一般消息
if(inmsg.startsWith("withWho")){
Stringshowmsg[]=inmsg.split("&");
System.out.println("接收者的名字:
"+showmsg[2]+"我的名字"+name+";\n");
if(showmsg[2].equals(name)){//如果是发给自己的消息
jTextArea2.append(showmsg[1]+"说:
"+showmsg[3]+"\n");
}
else{
jTextArea1.append(showmsg[1]+"说:
"+showmsg[3]+"\n");
}
}
}catch(Exceptionee){
System.out.println("Erroratrun()"+ee);
jTextArea2.append("与服务器连接中断,请重新登陆!
\n");
//输出流,输入流设置为null
in=null;
out=null;
return;
}
}
}
(4)服务器发送给所有人代码:
publicstaticvoidsendAll(Strings){
if(connections!
=null){
Enumeratione=connections.elements();
while(e.hasMoreElements()){
try{
PrintWriterpw=newPrintWriter(((Socket)e.nextElement()).getOutputStream());
pw.println(s);
pw.flush();
}
catch(IOExceptionex){}
}
}
System.out.println(s);
}
(5)服务器发送给指定人代码:
publicstaticbooleansendOne(Stringname,Stringmsg){
if(clients!
=null){
Enumeratione=clients.elements();
while(e.hasMoreElements()){
ClientProccp=(ClientProc)e.nextElement();//枚举所有连接中的用户
if((cp.getName()).equals(name)){
try{
PrintWriterpw=newPrintWriter((cp.getSocket()).getOutputStream());
pw.println(msg);
pw.flush();
returntrue;//找了返回且返回值为真
}catch(IOExceptionioe){}
}
}
}
returnfalse;//没有找到
}
(6)服务器发送更新用户信息代码:
privatevoidupdateList(){
//更新用户列表
Vectorcs=ChatServer.getClients();
if(cs!
=null){
for(Enumeratione=cs.elements();
e.hasMoreElements();){
ClientProccp=(ClientProc)e.nextElement();
Stringexist_name=cp.getName();
Stringexit_sex=cp.getSex();
out.println("old"+"&"+exist_name+"&"+exit_sex);//在这里标记以便判断
out.flush();
}
}
}
服务器处理接收到的信息:
publicvoidrun(){
while(name==null){
try{
Stringinmsg;
inmsg=in.readLine();//1111&Boy
ChatServer.sendAll("new"+"&"+inmsg);//发送信息更新用户列表new&1111&Boy
String[]userInfo;
userInfo=inmsg.split("&");
name=userInfo[0];
sex=userInfo[1];
//out.println("欢迎"+name);//初次登陆
//out.flush();
}catch(IOExceptionee){}
}
while(true){
try{//通过客户端发送代码到服务器来执行相应的功能,in.readLine()监视
Stringline=in.readLine();
String[]inmsg=line.split("&");
if(line.equals("quit")){//处理退出事件
ChatServer.sendAll("withWho&"+"【系统消息】&"+"所有人&"+this.name+"退出了聊天室");
ChatServer.deleteConnection(s,this);
return;
}
elseif(line.equals("refurbish")){//处理刷新用户列表请求
this.updateList();
}
elseif(line.startsWith("withWho")){
if(inmsg[2].equals("所有人"))
ChatServer.sendAll(line);
else{
if(ChatServer.sendOne(inmsg[2],line))
{
//out.println(line);
//out.flush();
}
else{
out.println("withWho&"+"【系统消息】&"+inmsg[1]+"&"+inmsg[2]+"已经退出了聊天室");
out.flush();//
}
}
}
else
ChatServer.sendAll(line);//发给所有的人
}catch(IOExceptione){
System.out.println("事件"+e);
try{
s.close();
}catch(IOExceptione2){}
return;
}
}
}
5.设计心得:
我自己感觉此次课程设计做得挺成功的,通过这一次,我学到了不少实际编程的知识。
在这次设计开始的第一天开始,我就开始着手修改一个书上的基于客户-服务器的java程序,这个程序的大框架给了我很大的启发。
我刚开始很自信自己能顺顺利利编写成功,但这一过程太不顺利。
用了两天时间来研究改写那个程序,但总是发现和我要实现的局域网聊天功能相差甚远。
后来我抛弃了那个例子,全部重写,磕磕绊绊的不断发现错误,并改正它们,功夫不负有心人人,我的程序最后运行成功了。