java课程设计实验报告.doc_第1页
java课程设计实验报告.doc_第2页
java课程设计实验报告.doc_第3页
java课程设计实验报告.doc_第4页
java课程设计实验报告.doc_第5页
已阅读5页,还剩52页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

课 程 设 计 报 告学院、系:吉林大学珠海学院计算机科学与技术系专业名称:计算机科学与技术课程设计科目java程序课程设计学生学号:04120420学生姓名:何星指导教师:董鑫正完成时间:2014 年 3月- 5月题目:20. 模仿QQ的部分功能,建立一个功能全面的网络聊天程序,可以实现多人聊天,并可以保存聊天记录。一、设计任务与目标 1)多线程聊天程序2)可以接收多人聊天3)可以发送文件4)可以发送图片5)可以保存聊天记录6)要求界面美观、布局合理、功能操作简便二、方案设计与论证网络编程的目的就是指直接或间接地通过网络协议与其他计算机进行通讯。网络编程中有两个主要的问题,一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输。在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可以唯一地确定Internet上的一台主机。而TCP层则提供面向应用的可靠的或非可靠的数据传输机制,这是网络编程的主要对象,一般不需要关心IP层是如何处理数据的。目前较为流行的网络编程模型是客户机/服务器(C/S)结构。即通信双方一方作为服务器等待客户提出请求并予以响应。客户则在需要服务时向服务器提出申请。服务器一般作为守护进程始终运行,监听网络端口,一旦有客户请求,就会启动一个服务进程来响应该客户,同时自己继续监听服务端口,使后来的客户也能及时得到服务。三、程序框图或流程图,程序清单与调用关系客户端发送与接收事件处理定义常用方法Run()接收并处理消息Actionperformed()按钮事件处理Insert()插入信息到自己的聊天区Keypressed()处理并发送消息itemStateChanged()判断私聊Putout()保存聊天记录Sendfilethread发送文件Shutdown()关闭窗口,向服务器发送离线请求Acceptfilethread接收文件createpopupMenu()清空聊天区调用关系Run()in.readLine()接受读取信息Split(“&”)处理信息,用于判断与识别Plist.listModel.addElement()向好友列表中加入好友Insert()信息插入自身聊天去This.acceptfileArea.setText()在接收文件曲线是要接收的文件Putout()聊天记录输出为文本以保存聊天记录Keypressed()inMsgField.getText()在输入框中获取信息Split(“&”)处理信息Out.println()发送信息Insert()插入信息到自身聊天区Putout()保存聊天记录SendfilethreadOut.println()发送客户信息和请求tos.read()从文件中读取数据doc.write()将数据写入网络缓冲区Inset()插入信息到自身聊天区Acceptfilethreadfile.createNewFile()将接受的文件重命名in.read()从网络缓冲区读数据raf.write()数据写入文件服务器调用关系定义方法Run()In.readline()接收信息sendOne()Split(“&”)判断处理信息sendAll()群发sendAll()deleteConnection()从用户组中删除用户addConnection()加入用户到用户组addConnection()Updatalist()更新好友列表sendOne()私聊deleteConnection()Updatalist()cp.getName()获取用户姓名cp.getSex()获取用户性别Out.println()向每个用户发送每个用户信息发送文件处理并发送信息insert()插入信息到自身聊天区doc.write()写入网络缓冲区fos.read()从文件读取数据Out.println()发送客户信息和请求Sendfilethread发送文件线程Actionperformed()按钮事件处理sendfileArea.getText()获取文件名ChatFrame()客户端界面Insert()插入信息到自己聊天区Out.println()输出信息inMsgField.getText()获取输入框信息Putout()保存聊天记录ChatFrame()客户端界面Keypressed()处理并发送信息YNYShoutdown()Link()If(e.getSource=ok)ChatFrame()客户端界面Connect()登录界面If(e.getSource()=cancell)接收客户端信息向客户端发送信息Updatalist()更新好友列表deleteConnection()从用户组中删除用户 addConnection()用户组中加入用户Split(“&”)判断处理信息sendOne()私聊sendAll()群发In.readline()接收信息run()处理和中转信息main()开启线程,启动服务器,监听客户端接收文件接收信息Raf.write()数据写入文件In.read()从网络缓冲区读数据file.createnewFile()接收并重命名文件Acceptfilethread接收文件线程Putout()保存聊天记录this.acceptfileArea.setText()显示要接收的文件到接收文件区Insert()信息插入到自身聊天区Plist.listModel.addElement()向好友列表中加入好友in.readLine()接受读取信息Run()接收并处理信息ChatFrame()客户端界面ChatFrame()客户端界面4、 全部源程序清单package chat;/import Client;import javax.swing.JFileChooser;import javax.swing.JProgressBar;import java.util.Date;import java.util.Calendar;import javax.swing.JTextArea; /import RTFReceiveFrame;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.Border;import javax.swing.text.BadLocationException;import javax.swing.text.SimpleAttributeSet;import javax.swing.text.StyleConstants;import javax.swing.text.StyledDocument;import .*;import java.io.*;import .Socket;SuppressWarnings(unused)public class ChatFrame extends JFrame implements ActionListener, ItemListener,Runnable, KeyListenersendfilethread sendthread;/ 发送文件线程;acceptfilethread acceptthread;/ 接收文件线程Socket acceptfilesocket; / 接收文件传输连接Socket sendfilesocket; / 发送文件传输连接ServerSocket fileserver; / 传输文件服务器String filemsg = null; / 保存文件输入流String sendfilename = null;/ 保存欲发送的文件名String IP;/ 保存本机IP= InetAddress.getLocalHost();/Boolean acceptboolean = false;Boolean sendboolean = false;int port = 6200;JProgressBar acceptProgressbar;/ 接收进度条JProgressBar sendProgressbar; / 发送进度条private Box leftbox = null;private Box rightbox = null;private Box leftrightbox = null;private Box rightleftbox = null;private JButton sendfile; / 发送文件按钮private JButton cancelsendfile; / 取消发送文件按钮private JButton acceptfile; / 接收文件按钮private JButton refusefile; / 拒绝接收文件按钮private MyTextArea sendfileArea = null; / 发送文件显示区private JTextPane acceptfileArea = null; / 接收文件显示区private static final long serialVersionUID = 1L;/ *菜单栏*private Box box = null; / 放输入组件的容器private JComboBox fontName = null, fontSize = null, fontStyle = null,fontColor = null;/ sendings = null;/ fontBackColor = null;private StyledDocument doc = null;private JFileChooser jfc;/ 文件保存路径选择器private JTextPane commonArea = null; / 公共发言区private JTextPane myMsgArea = null;/ 我的频道发言区public JComboBox perponsComboBox; / 下拉菜单private JTextArea inMsgField; / 发言输入框private JCheckBox privateTalk;/ 私聊checkboxprivate boolean privateTalkFlag = false; / 是否是私聊,默认值为假private JButton sentButton; / 发送消息按钮private JMenuItem menuItem;private JMenuItem cMenuItem;public BufferedReader in;public PrintWriter out;public String myName;private String withWho = 所有人;String outmsg;/ 发送的信息String mywords;/ 要说的话JPanel centerPanel;JScrollPane commonAreaScroll;JScrollPane myMsgAreaScroll;JScrollPane inMsgFieldScroll;public PList plist;public ChatFrame(String host)super(host + 的聊天室);try / 使用Windows的界面风格UIManager.setLookAndFeel(com.sun.java.swing.plaf.windows.WindowsLookAndFeel); catch (Exception e)e.printStackTrace();myName = host;plist = new PList(this);tryInetAddress addr = InetAddress.getLocalHost();IP = addr.getHostAddress().toString();/ 获得本机IP catch (Exception e)System.out.print(无法获取本地主机);/ System.out.println(本机的ip= + inet.getHostAddress();acceptProgressbar = new JProgressBar();acceptProgressbar.setOrientation(JProgressBar.HORIZONTAL);acceptProgressbar.setValue(0);acceptProgressbar.setStringPainted(true);sendProgressbar = new JProgressBar();sendProgressbar.setOrientation(JProgressBar.HORIZONTAL);sendProgressbar.setValue(0);sendProgressbar.setStringPainted(true);/ *聊天室右侧*/sendfile = new JButton(发送文件); cancelsendfile = new JButton(取消发送);acceptfile = new JButton(接收文件); refusefile = new JButton(拒绝文件); / 拒绝接收文件按钮acceptfileArea = new JTextPane(); / 接收文件显示区acceptfileArea.setEditable(false); / 不可从外部写sendfileArea = new MyTextArea(); / 发送文件显示区sendfileArea.setEditable(false);leftrightbox = Box.createHorizontalBox(); / 行结构leftrightbox.add(acceptfile, BorderLayout.WEST);leftrightbox.add(refusefile, BorderLayout.EAST);Box rightabove = Box.createVerticalBox();rightabove.add(leftrightbox);rightabove.add(acceptProgressbar);rightbox = Box.createVerticalBox();/ 竖结构rightbox.add(Box.createVerticalStrut(10);rightbox.add(rightabove);rightbox.add(Box.createVerticalStrut(10);JScrollPane inMsgFieldScroll1 = new JScrollPane(acceptfileArea);inMsgFieldScroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);inMsgFieldScroll1.setBorder(BorderFactory.createTitledBorder(待接收的文件);inMsgFieldScroll1.setBackground(new Color(188, 193, 199);rightbox.add(inMsgFieldScroll1, BorderLayout.CENTER);rightbox.setBackground(new Color(250, 0, 2);rightbox.add(Box.createVerticalStrut(10);rightleftbox = Box.createHorizontalBox();rightleftbox.add(sendfile, BorderLayout.WEST);rightleftbox.add(cancelsendfile, BorderLayout.EAST);Box rightmiddle = Box.createVerticalBox();rightmiddle.add(rightleftbox);rightmiddle.add(sendProgressbar);rightbox.add(rightmiddle, BorderLayout.CENTER);JScrollPane inMsgFieldScroll2 = new JScrollPane(sendfileArea);inMsgFieldScroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);inMsgFieldScroll2.setBorder(BorderFactory.createTitledBorder(欲发送的文件);inMsgFieldScroll2.setBackground(new Color(188, 193, 199);rightbox.add(inMsgFieldScroll2, BorderLayout.CENTER);rightbox.setBackground(new Color(0, 0, 255);/ *聊天室顶层*/JPanel upperPanel = new JPanel();String str_name = 宋体, 黑体, Dialog, Gulim ;String str_Size = 12, 14, 18, 22, 30, 40 ;String str_Style = 常规, 斜体, 粗体, 粗斜体 ;String str_Color = 黑色, 红色, 蓝色, 黄色, 绿色 ;fontName = new JComboBox(str_name); / 字体名称fontSize = new JComboBox(str_Size); / 字号fontStyle = new JComboBox(str_Style); / 样式fontColor = new JComboBox(str_Color); / 颜色box = Box.createVerticalBox(); / 竖结构Box box_1 = Box.createHorizontalBox(); / 横结构Box box_2 = Box.createVerticalBox(); / 横结构box.add(box_1);box.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8); / 8个的边距box_1.add(new JLabel(字体:); / 加入标签box_1.add(fontName); / 加入组件box_1.add(Box.createHorizontalStrut(8); / 间距box_1.add(new JLabel(样式:);box_1.add(fontStyle);box_1.add(Box.createHorizontalStrut(8);box_1.add(new JLabel(字号:);box_1.add(fontSize);box_1.add(Box.createHorizontalStrut(8);box_1.add(new JLabel(颜色:);box_1.add(fontColor);box_1.add(Box.createHorizontalStrut(8);box_1.add(Box.createHorizontalStrut(8);upperPanel.add(box, BorderLayout.SOUTH);/ *中间聊天室两个窗口*/Border brd = BorderFactory.createMatteBorder(/ 边框修饰色2, 2, 2, 1, new Color(125, 161, 253);centerPanel = new JPanel(new BorderLayout();commonArea = new JTextPane(); / 公共言论区commonArea.setBorder(brd);commonArea.setEditable(false); / 不可编辑commonArea.getScrollableUnitIncrement(new Rectangle(10, 20),SwingConstants.VERTICAL, -2);commonAreaScroll = new JScrollPane(commonArea);commonAreaScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);/ 设置滚动条什么时候出现commonAreaScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);commonAreaScroll.setBorder(BorderFactory.createTitledBorder(群聊区);box_2.add(commonAreaScroll);box_2.add(Box.createVerticalStrut(2);myMsgArea = new JTextPane(); / 我的发言myMsgArea.setBorder(brd);myMsgArea.setEditable(false);myMsgAreaScroll = new JScrollPane(myMsgArea);myMsgAreaScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);myMsgAreaScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);myMsgAreaScroll.setBorder(BorderFactory.createTitledBorder(悄悄话);box_2.add(myMsgAreaScroll);centerPanel.add(box_2);/ *输入发送区*JPanel centerLowerPanel = new JPanel(new BorderLayout();JPanel tempPanel1 = new JPanel(new BorderLayout();JPanel tempPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT);JLabel withWho = new JLabel(对);perponsComboBox = new JComboBox();perponsComboBox.addItem(所有人);privateTalk = new JCheckBox(私聊);inMsgField = new JTextArea(3, 2);inMsgField.setBorder(brd);inMsgField.setBackground(new Color(248, 243, 209);/ 设置聊天框体的颜色inMsgField.addKeyListener(this);sentButton = new JButton(发送);inMsgFieldScroll = new JScrollPane(inMsgField);inMsgFieldScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);inMsgFieldScroll.setBorder(BorderFactory.createTitledBorder(编辑区);tempPanel1.add(inMsgFieldScroll, BorderLayout.CENTER);sentButton.setBackground(Color.WHITE);tempPanel2.add(withWho);tempPanel2.add(new JLabel( );tempPanel2.add(perponsComboBox);tempPanel2.add(new JLabel( );tempPanel2.add(privateTalk);tempPanel2.add(new JLabel( );tempPanel2.add(new JLabel( );tempPanel2.add(sentButton);tempPanel2.add(new JLabel();centerLowerPanel.add(tempPanel1, BorderLayout.CENTER);centerLowerPanel.add(tempPanel2, BorderLayout.SOUTH);centerPanel.add(centerLowerPanel, BorderLayout.SOUTH);/ * 最下面的面板(South)*/JLabel BordBottomLabel = new JLabel();Icon BordBottom = new ImageIcon(imagesBordBottom.gif);BordBottomLabel.setIcon(BordBottom);leftbox = Box.createVerticalBox();/ 竖结构leftbox.add(upperPanel, BorderLayout.NORTH);leftbox.add(centerPanel, BorderLayout.CENTER);leftbox.add(BordBottomLabel, BorderLayout.SOUTH);this.add(leftbox, BorderLayout.CENTER);this.add(rightbox, BorderLayout.EAST);sentButton.addActionListener(this);/ 监听发送按钮sendfile.addActionListener(this);/ 监听文件发送按钮acceptfile.addActionListener(this);/ 监听文件接收按钮refusefile.addActionListener(this);/ 监听文件拒绝按钮cancelsendfile.addActionListener(this);/ 监听取消发送文件perponsComboBox.addActionListener(this);/ 监听下拉菜单privateTalk.addItemListener(this);/ 监听多选框状态this.createPopupMenu();inMsgField.requestFocus();this.setLocation(450, 50); / 居中this.setSize(600, 600);this.addWindowListener(new WindowAdapter() / 匿名类 监听窗口关闭时间public void windowClosing(WindowEvent event)shutDown(););this.setVisible(true);public void createPopupMenu()JPopupMenu popup = new JPopupMenu();menuItem = new JMenuItem(清空群聊区信息);menuItem.addActionListener(this);popup.add(menuItem); / 鼠标右击显示cMenuItem = new JMenuItem(清空私聊区信息);cMenuItem.addActionListener(this);popup.add(cMenuItem);/ Add listener to the text area so the popup menu can come up.MouseListener popupListener = new PopupListener(popup);/ PopupListener继承MouseAdaptercommonArea.addMouseListener(popupListener);myMsgArea.addMouseListener(popupListener);public void init(BufferedReader in, PrintWriter out)this.in = in;this.out = out;Thread th = new Thread(this);th.start();public void run()String inmsg;acceptthread = new acceptfilethread();while (true)tryif (inmsg = in.readLine() != null)if (inmsg.startsWith(old)String userInfo = inmsg.split(&);plist.listModel.addElement(userInfo1 + + userInfo2 + ); / 更新用户列表 else if (inmsg.startsWith(new) / 接收第一次服务器发送欢迎信息String userInfo = inmsg.split(&);plist.listModel.addElement(userInfo1 + + userInfo2 + ); / 更新用户列表 new & zhangsan &insert(commonArea, userInfo1 + 上线了); putout(gong.txt,userInfo1 + 上线了);insert(myMsgArea, userInfo1 + 上线了);putout(si.txt,userInfo1 + 上线了); else if (inmsg != null) / 一般消息String sendfile = inmsg.split(&);if (pareTo(cancelsendfile) = 0)insert(myMsgArea, sendfile1 + 取消了文件发送);putout(si.txt,sendfile1 + 取消了文件发送);acceptboolean = false;this.acceptfile.setEnabled(true);this.acceptfileArea.setText(); else if (pareTo(sendfile) = 0) / 如果是传送文件请求this.acceptfileArea.setText(sendfile2);insert(myMsgArea, sendfile1 + 发来文件);filemsg = inmsg; else if (pareTo(acceptfile) = 0) / 如果是传送文件请求insert(myMsgArea, sendfile1 + 接收了你发的文件);putout(si.txt,sendfile1 + 接收了你发的文件); else if (pareTo(refusefile) = 0) / 如果是传送文件请求insert(myMsgArea, sendfile1 + 拒绝了你发的文件);putout(si.txt,sendfile1 + 拒绝了你发的文件);sendboolean = false;this.sendfile.setEnabled(true);this.sendfileArea.setText(); else if (pareTo(withWho) = 0)if (sendfile2.equals(myName) / 如果是发给自己的消息insert(myMsgArea, sendfile1 + 对+ sendfile2 + 说: + sendfile3);putout(si.txt,sendfile1 + 对+ sendfile2 + 说: + sendfile3); / 显示到我的频道insert(commonArea, sendfile1 + 对 + sendfile2+ 说: + sendfile3);putout(gong.txt,sendfile1 + 对 + sendfile2 + 说: + sendfile3); else if (inmsg.startsWith(privateTalk)String showmsg = inmsg.split(&);if (showmsg1.equals(myName)/ 如果接收到的是我自己发送的消息insert(commonArea, 您对 + showmsg2 + 说: + showmsg3);putout(gong.txt,您对 + showmsg2 + 说: + showmsg3); else / 接收到的是别人发给我的消息(悄悄话)insert(myMsgArea, + showmsg1 + 对您说: + showmsg3);putout(si.txt, + showmsg1 + 对您说: + showmsg3); elseinsert(commonArea, inmsg); catch (Exception ee)ee.printStackTrace();insert(myMsgArea, 与服务器中断,请重新登录!);in = null;out = null;return;/ /*发送文件线程*/class sendfilethread extends Thread/ ActionListenerkhhkh压郁sendfilethread()/ 构造函数public void run()File file = new File(sendfilename);FileInputStream fos = null;tryfos = new FileInputStream(file); catch (IOException e1)System.out.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论