《面向对象程序设计课程设计》Word格式文档下载.docx

上传人:b****1 文档编号:445989 上传时间:2023-04-28 格式:DOCX 页数:29 大小:113.23KB
下载 相关 举报
《面向对象程序设计课程设计》Word格式文档下载.docx_第1页
第1页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第2页
第2页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第3页
第3页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第4页
第4页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第5页
第5页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第6页
第6页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第7页
第7页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第8页
第8页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第9页
第9页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第10页
第10页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第11页
第11页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第12页
第12页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第13页
第13页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第14页
第14页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第15页
第15页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第16页
第16页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第17页
第17页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第18页
第18页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第19页
第19页 / 共29页
《面向对象程序设计课程设计》Word格式文档下载.docx_第20页
第20页 / 共29页
亲,该文档总共29页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

《面向对象程序设计课程设计》Word格式文档下载.docx

《《面向对象程序设计课程设计》Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《《面向对象程序设计课程设计》Word格式文档下载.docx(29页珍藏版)》请在冰点文库上搜索。

《面向对象程序设计课程设计》Word格式文档下载.docx

在聊天的时候可以更换背景色,可以统计在线人数。

还有停止服务器和退出服务器功能。

当开启服务器的停止功能后,客户端就不能进行交互。

服务器端还可以增加其他的功能,比如统计客户在线人数,实现客户端之间的私聊功能等。

客户端的功能设计是先运行客户端,然后登录连接服务器,当连接成功后便可以进行聊天功能。

客户端还可以增加其他的功能,比如,需要客户先进行注册,注册成功后再登录连接服务器才能进行聊天功能等。

2.1.1.聊天室服务器端模块主要有以下几部分组成

2.1.1.1.主框架类(MDS_ServerChat.java)

该文件包含名为MDS_ServerChat的public类,其主要功能是定义服务器的界面。

添加事件侦听,事件处理。

利用套接字ServerSocket和socket的对象进行消息的收发。

2.1.1.2.服务器用户上线侦听类.

该类实现了Runnable类,重写了Runnable类中的run方法,在run方法中实现对服务器用户的上线侦听。

2.1.1.3.服务器的启动和发送类。

该类实现了ActionListener类,重写了ActionListener类的actionPerformed方法。

在actionPerformed方法中实现对服务器的启动。

实现对服务器消息的发送工作。

2.1.1.4.服务器的接收类。

该类实现了Runnable类,重写了Runnable类中的run方法,在run方法中实现对服务器用户消息的接收工作。

2.1.2.聊天室客户端模块主要有以下几部分组成

2.1.2.1.主框架类(MDS_ClientChat.java)

该文件包含名为MDS_ClientChat的public类,其主要功能是定义客户端的界面。

2.1.2.2向客户端发送消息类。

在actionPerformed方法中实现对客户端的启动和登录服务器及向服务器发送消息的工作。

如果增加了多个客户端,还可以和其他客户端进行交互等。

2.1.2.3.客户端接收消息类。

该类实现了Runnable类,调用了Runnable类中的run方法来实现对服务器发过来的消息进行接收工作。

如果还增加了其他的客户端,那么也可以和其他的客户端进行交互,实现私聊功能。

2.2.主要类说明。

类、接口

功能

JFrame

JFrame类的实例就是一个底层容器,即通常所说的窗口。

在该程序中是MDS_ClientChat类的父类。

MDS_ClientChat

其主要功能是定义服务器的界面。

ActionListener

ActionListener接口中只有一个actionPerformed()方法。

当发生ActionEvent事件时,监视器调用接口中的actionPerformed(ActionEvente)方法对发生的事件作出处理

Runnable

声明一个实现Runnable接口的类并实现run()方法。

在调用start()方法时,将创建一个新的控制线程,接着它将调用run()方法。

run()方法中的代码定义执行线程所需的功能

Socket

在套接字通信中客户端的程序使用Socket类建立与服务器套接字连接

ServerSocket

服务器端程序使用ServerSocket类建立接收客户套接字的服务器套接字

2.3.检查流程设计。

等待客户端的连接连接服务器

服务器已断开,请重新连接

3.系统实现。

(附上具体源代码)

3.1.MDS_ServerChat.java

packagemadingsheng;

importjava.awt.*;

importjavax.swing.*;

importjava.awt.event.*;

import.*;

importjava.io.*;

publicclassMDS_ServerChatextendsJFrameimplementsActionListener,Runnable

{

privateintport=8888;

privateServerSocketserver;

privateSocketclient;

privateBufferedReadercin;

privatePrintWritercout;

privateThreadchatThread;

privateJPanelp1;

privateJButtonstartButton;

privateJButtonstopButton;

privateJButtonexitButton;

privateJScrollPanescrollPanel;

privateJTextAreamessage;

privateJPanelp2;

privateJTextFieldserverMessage;

privateJButtonmessageButton;

privateJPanelp3;

privateJComboBox<

String>

combox1;

combox2;

privateJMenuBarmenubar;

privateJMenumenu,menu1;

privateJMenuItemitem1,item2,item3,item4,item5;

publicMDS_ServerChat()

{

super("

网络聊天服务器"

);

menubar=newJMenuBar();

menu=newJMenu("

更换背景色"

menu1=newJMenu("

帮助"

item1=newJMenuItem("

灰色"

item2=newJMenuItem("

蓝色"

item3=newJMenuItem("

白色"

item4=newJMenuItem("

时间"

item5=newJMenuItem("

其他"

menu.add(item1);

menu.add(item2);

menu.add(item3);

menu1.add(item4);

menu1.add(item5);

menubar.add(menu);

menubar.add(menu1);

setJMenuBar(menubar);

item1.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

message.setBackground(Color.gray);

}

}

);

item2.addActionListener(newActionListener()

message.setBackground(Color.blue);

item3.addActionListener(newActionListener()

message.setBackground(Color.white);

p1=newJPanel();

startButton=newJButton("

启动"

stopButton=newJButton("

停止"

exitButton=newJButton("

退出"

startButton.addActionListener(this);

stopButton.addActionListener(this);

exitButton.addActionListener(this);

p1.add(startButton);

p1.add(stopButton);

p1.add(exitButton);

add(p1,BorderLayout.NORTH);

message=newJTextArea();

scrollPanel=newJScrollPane(message,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

add(scrollPanel,BorderLayout.CENTER);

p2=newJPanel();

serverMessage=newJTextField(15);

messageButton=newJButton("

发送消息"

messageButton.addActionListener(this);

p2.add(serverMessage);

p2.add(messageButton);

add(p2,BorderLayout.SOUTH);

p3=newJPanel();

combox1=newJComboBox<

();

combox2=newJComboBox<

combox1.addItem("

--在线人数--"

不祥"

不详"

p3.add(combox1);

add(p3,BorderLayout.EAST);

p3.setBackground(Color.gray);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(800,100,500,400);

setVisible(true);

}

publicvoidactionPerformed(ActionEvente)

if(e.getSource()==startButton)

p1.setBackground(Color.green);

try

server=newServerSocket(port);

//client=server.accept();

catch(Exceptionex)

chatThread=newThread(this);

chatThread.start();

startButton.setVisible(false);

message.append("

服务器已经启动"

+"

\n"

elseif(e.getSource()==stopButton)

chatThread=null;

startButton.setVisible(true);

if(cout!

=null)

cout.println("

stop"

if(server!

{

p1.setBackground(Color.red);

server.close();

}

catch(Exceptionexp)

elseif(e.getSource()==exitButton)

exit"

System.exit(0);

elseif(e.getSource()==messageButton)

if(chatThread!

cout.println(serverMessage.getText()+"

message.append("

服务器发送了:

"

+serverMessage.getText()+"

publicvoidrun()

while(true)

client=server.accept();

ServerChatThreadsct=newServerChatThread(client);

Threadt=newThread(sct);

t.start();

publicstaticvoidmain(String[]args)

MDS_ServerChatserverChat=newMDS_ServerChat();

classServerChatThreadimplementsRunnable

privateSocketclient2;

ServerChatThread(Socketclient)

client2=client;

publicvoidrun()

while(true)

try

cin=newBufferedReader(newInputStreamReader(client2.getInputStream()));

cout=newPrintWriter(client2.getOutputStream(),true);

Stringusername=cin.readLine();

if(username!

{

message.append("

客户:

+username+"

进入"

cout.println("

ok"

}

Stringaline="

;

while(chatThread!

aline=cin.readLine();

if(aline.equals("

))

{

break;

}

else

message.append(username+"

说:

+aline);

catch(Exceptione)

}

3.2.MDS_ClientChat.java

publicclassMDS_ClientChatextendsJFrameimplementsActionListener,Runnable

privateJLabelusernameLabel;

privateJTextFieldusername;

privateJButtonloginButton;

privateJTextFieldclientMessage;

privateJButtonclientButton;

publicMDS_ClientChat()

网络聊天客户端"

usernameLabel=newJLabel("

用户名"

username=newJTextField(10);

loginButton=newJButton("

登录"

loginButton.addActionListener(this);

p1.add(usernameLabel);

p1.add(username);

p1.add(loginButton);

clientMessage=newJTextField(15);

clientButton=newJButton("

clientButton.addActionListener(this);

p2.add(clientMessage);

p2.add(clientButton);

setBounds(100,100,500,400);

if(e.getSource

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

当前位置:首页 > 自然科学 > 天文地理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2