利用JAVA实现简单聊天室.docx

上传人:b****3 文档编号:5532544 上传时间:2022-12-18 格式:DOCX 页数:23 大小:123.49KB
下载 相关 举报
利用JAVA实现简单聊天室.docx_第1页
第1页 / 共23页
利用JAVA实现简单聊天室.docx_第2页
第2页 / 共23页
利用JAVA实现简单聊天室.docx_第3页
第3页 / 共23页
利用JAVA实现简单聊天室.docx_第4页
第4页 / 共23页
利用JAVA实现简单聊天室.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

利用JAVA实现简单聊天室.docx

《利用JAVA实现简单聊天室.docx》由会员分享,可在线阅读,更多相关《利用JAVA实现简单聊天室.docx(23页珍藏版)》请在冰豆网上搜索。

利用JAVA实现简单聊天室.docx

利用JAVA实现简单聊天室

利用JAVA实现简单聊天室

1.设计思路

Java是一种简单的,面向对象的,分布式的,解释的,键壮的,安全的,结构中立的,可移植的,性能很优异的,多线程的,动态的语言。

而且,Java很小,整个解释器只需215K的RAM。

因此运用JAVA程序编写聊天室,实现简单聊天功能。

程序实现了聊天室的基本功能,其中有:

(1)启动服务器:

实现网络的连接,为注册进入聊天室做准备。

(2)注册登陆界面:

填写基本信息如姓名等,可以供多人进入实现多人聊天功能。

(3)发送信息:

为用户发送信息提供平台。

(4)离开界面:

使用户退出聊天室。

(5)关闭服务器:

断开与网络的连接,彻底退出聊天室。

?

2.设计方法

在设计简单聊天室时,需要编写5个Java源文件:

、、、、。

3程序功能图及程序相关说明

(1)主功能框图

 

(2)聊天室基本功能表

序号

文件名

主要功能

1

ServerHost

;

被服务器调用

2

Client

显示聊天室

3

ClientUser

被客户端调用

4

LogIn

注册登陆

5

Objecting

连接服务器

6

Server

启动服务器

 

 

4.程序代码是说明

程序中引入的包:

packageChat;

import.*;

import.*;

import.*;

<

import.*;

import.*;

(1)服务器端代码中用户自定义类:

类名:

Server

作用:

服务器启动

继承的接口名:

ActionListener

publicclassServerimplementsActionListener{

定义的对象:

count计结果与分析

5个Java源文件:

、、、、。

分别编译这5个Java源文件,或运行“javac*.java”命令,编译全部的源文件,然后运行连接服务器的主类,即运行Server类。

连接完毕后在运行注册的主函数类,即运行LogIn类。

~

首先运行启动服务器的文件,运行结果如下:

图1服务器启动成功

结果分析:

在进入聊天室之前必须先启动服务器,实现服务器端的连接为用户的登录打开“窗口”。

其次运行用户注册登录文件,运行结果如下:

 

图2用户登陆界面

结果分析:

添加用户信息,输入用户姓名,为用户进入聊天室提供关口。

并且利用以下语句实现如图3的界面JLabeltitle=newJLabel("免注册版");

JLabelname=newJLabel("输入姓名");

JButtonsend=newJButton("进入");

添加注册信息后点击“进入”按钮,结果如图3

图3显示用户已登录

结果分析:

当服务器启动用户完成信息注册时,在服务期端的界面中会显示当前在线的人员,并且提示客户已连接到服务器,可以进行聊天行为。

图4显示聊天内容

结果分析:

多个用户进入聊天室之后,用户可以在发送信息的文本框里填写自己的内容,点击“发送信息”按钮,信息会出现在上方的区域内,在界面的右方还可以显示在线人员的姓名,另在线用户可以知道有谁在聊天室内。

聊天结束点击“离开”按钮,结果如图5所示:

图5离开聊天室

点击服务器中的“停止”按钮,结果如图6.图7.所示:

图6点击“停止”按钮

图7终止与服务器端连接

结果分析:

用户聊天结束点击“离开”按钮之后会在服务器端的界面上自动显示该用户已离开,提时、示服务器执行相关行为。

离开界面:

使用户退出聊天室;关闭服务器:

断开与网络的连接,彻底退出聊天室。

6.语句实现

(1)

packageChat;

import.*;

import.*;

import.*;

?

import.*;

import.*;

publicclassServerimplementsActionListener{

intcount=0;

Vectorsoconly=newVector();

HashMapsockets=newHashMap();

HashMapsocket_thread=newHashMap();

ServerSocketserversocket;

JFrameframe;

JTextAreaarea=newJTextArea();

JPanelpanel=newJPanel();

JButtonstart=newJButton("启动");

JButtonstop=newJButton("停止");

publicServer(){

(start);

(stop);

(panel,;

(area,;

();

center();

'

;

(true);

(this);

(this);

"new");

}

publicvoidcenter(){

Toolkittoolkit=();

Dimensione=();

*

intx=2-(int)().getWidth()/2);

inty=2-(int)().getHeight()/2);

(x,y);

}

publicvoidactionPerformed(ActionEvente){

JButtonbtn=(JButton)();

if(btn==start){

serverRunserverrun=newserverRun();

"gogo");

();

%

}

if(btn==stop){

try{

Iteratori=();

while()){

Sockets=(Socket)();

ObjectOutputStreamout=newObjectOutputStream(s.

getOutputStream());

Objectingbj=newObjecting();

("系统消息-----服务器马上关闭!

请马上关闭客户端");

;

}

count++;

if(count==2){

(0);

}

(frame,"已通知客户端,请再次点击关闭按钮关闭");

}catch(Exceptionex){();}

}

}

classserverRunextendsThread{

?

publicvoidrun(){

try{

"开始SERVERRUN");

serversocket=newServerSocket(55555);

("服务器启动\n");

(false);

while(true){

Socketsocket=();

"连接成功");

BufferedReaderbr=newBufferedReader(newInputStreamReader()));

Stringname=();

("服务器接收用户"+name+"的连接\n");

(socket);

Iteratori=();

while()){

Sockets=(Socket)();;

import.*;

importclassLogInimplementsActionListener{

JFrameframe;

JLabeltitle=newJLabel("免注册版");

JPanelbanner=newJPanel();

JTextFieldtext=newJTextField(10);

JPanelbody=newJPanel();

JPanelbuttom=newJPanel();

JLabelname=newJLabel("输入姓名");

JButtonsend=newJButton("进入");

publicLogIn(){

frame=newJFrame("注册");

;

(title);

'

(name);

(text);

(send);

(banner,;

(body,;

(buttom,;

();

center();

(true);

(this);}

publicvoidcenter(){

Toolkittoolkit=();

Dimensione=();

intx=2-(int)().getWidth()/2);

inty=2-(int)().getHeight()/2);

(x,y);}

publicvoidactionPerformed(ActionEvente){

if().equals("")){

(frame,"姓名不能为空");

return;

'

}else{

newClient());

();}}

publicstaticvoidmain(Stringargs[]){

newLogIn();}}

(4)

packageChat;

import.*;

import.*;

import.*;

@

import.*;

publicclassClientUserextendsWindowAdapterimplementsActionListener,Runnable{

publicJFramejf;

publicJTextFieldjt;

publicJTextFieldname;

publicJButtonconnect;

publicJScrollPanejsp;

publicJTextAreajta;

publicJButtonjb;

publicJLabell;

*

Socketsoc;

InputStreamsoc_in;

OutputStreamsoc_out;

BufferedReaderBsoc_in;

PrintWriterPsoc_out;

publicClientUser(){

jf=newJFrame("ClientChatRoom");

jta=newJTextArea(10,30);

(false);

jsp=newJScrollPane(jta,,;

jt=newJTextField(30);

name=newJTextField(5);

l=newJLabel("Pleaseinputyourname");

connect=newJButton("Connect");

(this);

(this);

jb=newJButton("SendTheMessage");

(this);

JPaneljp=newJPanel();

JPaneljp2=newJPanel();

[

(newFlowLayout());

(jt);

(jb);

(l);

(name);

(connect);

Containerc=();

(newGridBagLayout());

GridBagConstraintsgbc=newGridBagConstraints();

=0;

=0;

(jp2,gbc);

=0;

=1;

(jsp,gbc);

=2;

(jp,gbc);

();

(true);}

publicstaticvoidmain(String[]args){

ClientUsercuser=newClientUser();}

publicvoidwindowClosing(WindowEvente){

(0);}

publicvoidactionPerformed(ActionEvente){

if()==connect){

if().equals("")){

newJOptionPane().showMessageDialog(null,"PleaseinPutyournameFirst!

");

}

else{

try{

^

soc=newSocket("localhost",55555);

Psoc_out=newPrintWriter(),true);

Bsoc_in=newBufferedReader(newInputStreamReader()));

());

}

catch(Exceptione1){();}

(false);

newThread(this).start();

}}

if()==jt||()==jb){

'

()+":

"+()+"\n");

()+":

"+());

("");

}}

publicvoidrun(){

Stringtext;

while(true){

try{

text=();

(text+"\n");

`

}

catch(Exceptione){();}

}}

(5)

packageChat;

import.*;

import.*;

import.*;

import.*;

importclassClientimplementsActionListener{

`

ClientSocketcs;

Stringname;

Socketsocket;

JFrameframe;

JTextAreaarea=newJTextArea(30,30);

JTextFieldtext=newJTextField(30);

JButtonsend=newJButton("发送消息");

JButtonexit=newJButton("离开");

JPanelbanner=newJPanel();

JPanelpanel=newJPanel();

Listlist=newList(35);

JPanelright=newJPanel();

JLabelwelcome=newJLabel();

publicClient(Stringname2){

=name2;

(name+"----欢迎来到聊天室");

(false);

frame=newJFrame("聊天室");

;

(false);

<

(welcome);

(banner,;

(text);

(send);

(exit);

(panel,;

(area,;

(newJScrollPane(list));

(right,;

();

center();

(true);

connect();

(this);

(this);

(newActionListener(){

publicvoidactionPerformed(ActionEvente){

Stringto=();

Stringcontent=(frame,"输入向"+name+"发送的私聊信息","私聊",;

<

if(content==null){

return;

}else{

Objectingo=newObjecting(to,"---私聊信息--"+name+":

"+content);

try{

ObjectOutputStreamout=newObjectOutputStream());

(o);

(frame,to+"收到消息");

}catch(Exceptione2){();}

}

}

});

(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if().equals("")){

(frame,"消息为空");

return;

}else{

Objectingo=newObjecting("all",name+":

"+())

*

try{

ObjectOutputStreamout=newObjectOutputStream());

(o);

("");

}catch(Exceptione2){();}

}}});}

publicvoidconnect(){

try{

socket=newSocket("localhost",55555);

cs=newClientSocket();

();

}catch(Exceptione){();}}

publicvoidcenter(){

Toolkittoolkit=();

Dimensione=();

intx=2-(int)().getWidth()/2);

inty=2-(int)().getHeight()/2);

(x,y);

}

classClientSocketextendsThread{

ObjectInputStreamin;

ObjectOutputStreamout;

PrintWriterpw;

publicvoidrun(){

try{

pw=newPrintWriter(),true);

(name);

}catch(Exceptione){();}

while(true){

try{

in=newObjectInputStream());

Objecto=();

if(oinstanceofString){

()+"\n");}

if(oinstanceofVector){

"列表操作");

Vectorv=(Vector)o;

();

Object[]obj=();

for(inti=0;i<;i++){

%

((String)obj[i]);

}}

}catch(Exceptione){

();

}}}}

publicvoidactionPerformed(ActionEvente){

JButtonbtn=(JButton)();

if(btn==send){

if().equals("")){

$

(frame,"消息为空");

return;

}else{

Objectingo=newObjecting("all",name+":

"+());

try{

ObjectOutputStreamout=newObjectOutputStream());

(o);

("");

}catch(Exceptione2){();}

}

}

if(btn==exit){

Objectingo=newObjecting("exit",name);

try{

ObjectOutputStreamout=newObjectOutputStream());

(o);

();

();

}catch(Exceptionx){();}

(0);

~

}}}

(5)

packageChat;

import.*;

import.*;

import.*;

import.*;

import.*;

publicclassServerHostextendsWindowAdapterimplementsActionListener,Runnable{

publicJFramejf;

publicJTextFieldjt;

publicJScrollPanejsp;

publicstaticJTextAreajta;

publicJButtonjb;

HashMaphm=newHashMap();

JLabelinof;

JTextFieldname;

JCheckBoxcheck;

OutputStreamsoc_out;

VectorThread_vector=newVector();

|

publicServerHost(){

jf=newJFrame("ServerChatRoom");

jta=newJTextArea(10,30);

(false);

jsp=newJScrollPane(jta,,;

jt=newJTextField(30);

(this);

jb=newJButton("SendTheMessage");

(this);

check=newJCheckBox("Sendtoall");

(true);

inof=newJLabel("writethenameyouwanttosend");

name=newJTextField(5);

(false);

JPaneljp=newJPanel();

(newFlowLayout());

(inof);

(name);

(jt);

(jb);

(check);

Containerc=();

(newGridBagLayout());

GridBagConstraintsgbc=newGridBagConstraints();

=0;

=0;

(jsp,gbc);

=1;

(jp,gbc);

();

(true);}

publicvoidwindowClosing(WindowEvente){

(0);}

publicstaticvoidmain(String[]args){

ServerHostsh=newServerHost();

newThread(sh).start();}

publicvoidrun(){

ServerSocketssc=null;

try{

ssc=newServerSocket(55555);

}

catch(Exceptione){"1");}

while(true){

try{

Socketsoc=();

BufferedReader

Bsoc_in=newBufferedReader(newInputStreamReader()));

Strings=();

\

(soc,s);

ServerThreadst

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1