java課程設計實驗報告.doc_第1頁
java課程設計實驗報告.doc_第2頁
java課程設計實驗報告.doc_第3頁
java課程設計實驗報告.doc_第4頁
java課程設計實驗報告.doc_第5頁
已閱讀5頁,還剩52頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

課 程 設 計 報 告學院、系:吉林大學珠海學院計算機科學與技術系專業(yè)名稱:計算機科學與技術課程設計科目java程序課程設計學生學號:04120420學生姓名:何星指導教師:董鑫正完成時間:2014 年 3月- 5月題目:20. 模仿QQ的部分功能,建立一個功能全面的網(wǎng)絡聊天程序,可以實現(xiàn)多人聊天,并可以保存聊天記錄。一、設計任務與目標 1)多線程聊天程序2)可以接收多人聊天3)可以發(fā)送文件4)可以發(fā)送圖片5)可以保存聊天記錄6)要求界面美觀、布局合理、功能操作簡便二、方案設計與論證網(wǎng)絡編程的目的就是指直接或間接地通過網(wǎng)絡協(xié)議與其他計算機進行通訊。網(wǎng)絡編程中有兩個主要的問題,一個是如何準確的定位網(wǎng)絡上一臺或多臺主機,另一個就是找到主機后如何可靠高效的進行數(shù)據(jù)傳輸。在TCP/IP協(xié)議中IP層主要負責網(wǎng)絡主機的定位,數(shù)據(jù)傳輸?shù)穆酚?,由IP地址可以唯一地確定Internet上的一臺主機。而TCP層則提供面向應用的可靠的或非可靠的數(shù)據(jù)傳輸機制,這是網(wǎng)絡編程的主要對象,一般不需要關心IP層是如何處理數(shù)據(jù)的。目前較為流行的網(wǎng)絡編程模型是客戶機/服務器(C/S)結構。即通信雙方一方作為服務器等待客戶提出請求并予以響應。客戶則在需要服務時向服務器提出申請。服務器一般作為守護進程始終運行,監(jiān)聽網(wǎng)絡端口,一旦有客戶請求,就會啟動一個服務進程來響應該客戶,同時自己繼續(xù)監(jiān)聽服務端口,使后來的客戶也能及時得到服務。三、程序框圖或流程圖,程序清單與調(diào)用關系客戶端發(fā)送與接收事件處理定義常用方法Run()接收并處理消息Actionperformed()按鈕事件處理Insert()插入信息到自己的聊天區(qū)Keypressed()處理并發(fā)送消息itemStateChanged()判斷私聊Putout()保存聊天記錄Sendfilethread發(fā)送文件Shutdown()關閉窗口,向服務器發(fā)送離線請求Acceptfilethread接收文件createpopupMenu()清空聊天區(qū)調(diào)用關系Run()in.readLine()接受讀取信息Split(“&”)處理信息,用于判斷與識別Plist.listModel.addElement()向好友列表中加入好友Insert()信息插入自身聊天去This.acceptfileArea.setText()在接收文件曲線是要接收的文件Putout()聊天記錄輸出為文本以保存聊天記錄Keypressed()inMsgField.getText()在輸入框中獲取信息Split(“&”)處理信息Out.println()發(fā)送信息Insert()插入信息到自身聊天區(qū)Putout()保存聊天記錄SendfilethreadOut.println()發(fā)送客戶信息和請求tos.read()從文件中讀取數(shù)據(jù)doc.write()將數(shù)據(jù)寫入網(wǎng)絡緩沖區(qū)Inset()插入信息到自身聊天區(qū)Acceptfilethreadfile.createNewFile()將接受的文件重命名in.read()從網(wǎng)絡緩沖區(qū)讀數(shù)據(jù)raf.write()數(shù)據(jù)寫入文件服務器調(diào)用關系定義方法Run()In.readline()接收信息sendOne()Split(“&”)判斷處理信息sendAll()群發(fā)sendAll()deleteConnection()從用戶組中刪除用戶addConnection()加入用戶到用戶組addConnection()Updatalist()更新好友列表sendOne()私聊deleteConnection()Updatalist()cp.getName()獲取用戶姓名cp.getSex()獲取用戶性別Out.println()向每個用戶發(fā)送每個用戶信息發(fā)送文件處理并發(fā)送信息insert()插入信息到自身聊天區(qū)doc.write()寫入網(wǎng)絡緩沖區(qū)fos.read()從文件讀取數(shù)據(jù)Out.println()發(fā)送客戶信息和請求Sendfilethread發(fā)送文件線程Actionperformed()按鈕事件處理sendfileArea.getText()獲取文件名ChatFrame()客戶端界面Insert()插入信息到自己聊天區(qū)Out.println()輸出信息inMsgField.getText()獲取輸入框信息Putout()保存聊天記錄ChatFrame()客戶端界面Keypressed()處理并發(fā)送信息YNYShoutdown()Link()If(e.getSource=ok)ChatFrame()客戶端界面Connect()登錄界面If(e.getSource()=cancell)接收客戶端信息向客戶端發(fā)送信息Updatalist()更新好友列表deleteConnection()從用戶組中刪除用戶 addConnection()用戶組中加入用戶Split(“&”)判斷處理信息sendOne()私聊sendAll()群發(fā)In.readline()接收信息run()處理和中轉(zhuǎn)信息main()開啟線程,啟動服務器,監(jiān)聽客戶端接收文件接收信息Raf.write()數(shù)據(jù)寫入文件In.read()從網(wǎng)絡緩沖區(qū)讀數(shù)據(jù)file.createnewFile()接收并重命名文件Acceptfilethread接收文件線程Putout()保存聊天記錄this.acceptfileArea.setText()顯示要接收的文件到接收文件區(qū)Insert()信息插入到自身聊天區(qū)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;/ 發(fā)送文件線程;acceptfilethread acceptthread;/ 接收文件線程Socket acceptfilesocket; / 接收文件傳輸連接Socket sendfilesocket; / 發(fā)送文件傳輸連接ServerSocket fileserver; / 傳輸文件服務器String filemsg = null; / 保存文件輸入流String sendfilename = null;/ 保存欲發(fā)送的文件名String IP;/ 保存本機IP= InetAddress.getLocalHost();/Boolean acceptboolean = false;Boolean sendboolean = false;int port = 6200;JProgressBar acceptProgressbar;/ 接收進度條JProgressBar sendProgressbar; / 發(fā)送進度條private Box leftbox = null;private Box rightbox = null;private Box leftrightbox = null;private Box rightleftbox = null;private JButton sendfile; / 發(fā)送文件按鈕private JButton cancelsendfile; / 取消發(fā)送文件按鈕private JButton acceptfile; / 接收文件按鈕private JButton refusefile; / 拒絕接收文件按鈕private MyTextArea sendfileArea = null; / 發(fā)送文件顯示區(qū)private JTextPane acceptfileArea = null; / 接收文件顯示區(qū)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; / 公共發(fā)言區(qū)private JTextPane myMsgArea = null;/ 我的頻道發(fā)言區(qū)public JComboBox perponsComboBox; / 下拉菜單private JTextArea inMsgField; / 發(fā)言輸入框private JCheckBox privateTalk;/ 私聊checkboxprivate boolean privateTalkFlag = false; / 是否是私聊,默認值為假private JButton sentButton; / 發(fā)送消息按鈕private JMenuItem menuItem;private JMenuItem cMenuItem;public BufferedReader in;public PrintWriter out;public String myName;private String withWho = 所有人;String outmsg;/ 發(fā)送的信息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(發(fā)送文件); cancelsendfile = new JButton(取消發(fā)送);acceptfile = new JButton(接收文件); refusefile = new JButton(拒絕文件); / 拒絕接收文件按鈕acceptfileArea = new JTextPane(); / 接收文件顯示區(qū)acceptfileArea.setEditable(false); / 不可從外部寫sendfileArea = new MyTextArea(); / 發(fā)送文件顯示區(qū)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(欲發(fā)送的文件);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 = 常規(guī), 斜體, 粗體, 粗斜體 ;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(); / 公共言論區(qū)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);/ 設置滾動條什么時候出現(xiàn)commonAreaScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);commonAreaScroll.setBorder(BorderFactory.createTitledBorder(群聊區(qū));box_2.add(commonAreaScroll);box_2.add(Box.createVerticalStrut(2);myMsgArea = new JTextPane(); / 我的發(fā)言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);/ *輸入發(fā)送區(qū)*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(發(fā)送);inMsgFieldScroll = new JScrollPane(inMsgField);inMsgFieldScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);inMsgFieldScroll.setBorder(BorderFactory.createTitledBorder(編輯區(qū));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);/ 監(jiān)聽發(fā)送按鈕sendfile.addActionListener(this);/ 監(jiān)聽文件發(fā)送按鈕acceptfile.addActionListener(this);/ 監(jiān)聽文件接收按鈕refusefile.addActionListener(this);/ 監(jiān)聽文件拒絕按鈕cancelsendfile.addActionListener(this);/ 監(jiān)聽取消發(fā)送文件perponsComboBox.addActionListener(this);/ 監(jiān)聽下拉菜單privateTalk.addItemListener(this);/ 監(jiān)聽多選框狀態(tài)this.createPopupMenu();inMsgField.requestFocus();this.setLocation(450, 50); / 居中this.setSize(600, 600);this.addWindowListener(new WindowAdapter() / 匿名類 監(jiān)聽窗口關閉時間public void windowClosing(WindowEvent event)shutDown(););this.setVisible(true);public void createPopupMenu()JPopupMenu popup = new JPopupMenu();menuItem = new JMenuItem(清空群聊區(qū)信息);menuItem.addActionListener(this);popup.add(menuItem); / 鼠標右擊顯示cMenuItem = new JMenuItem(清空私聊區(qū)信息);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) / 接收第一次服務器發(fā)送歡迎信息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 + 取消了文件發(fā)送);putout(si.txt,sendfile1 + 取消了文件發(fā)送);acceptboolean = false;this.acceptfile.setEnabled(true);this.acceptfileArea.setText(); else if (pareTo(sendfile) = 0) / 如果是傳送文件請求this.acceptfileArea.setText(sendfile2);insert(myMsgArea, sendfile1 + 發(fā)來文件);filemsg = inmsg; else if (pareTo(acceptfile) = 0) / 如果是傳送文件請求insert(myMsgArea, sendfile1 + 接收了你發(fā)的文件);putout(si.txt,sendfile1 + 接收了你發(fā)的文件); else if (pareTo(refusefile) = 0) / 如果是傳送文件請求insert(myMsgArea, sendfile1 + 拒絕了你發(fā)的文件);putout(si.txt,sendfile1 + 拒絕了你發(fā)的文件);sendboolean = false;this.sendfile.setEnabled(true);this.sendfileArea.setText(); else if (pareTo(withWho) = 0)if (sendfile2.equals(myName) / 如果是發(fā)給自己的消息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)/ 如果接收到的是我自己發(fā)送的消息insert(commonArea, 您對 + showmsg2 + 說: + showmsg3);putout(gong.txt,您對 + showmsg2 + 說: + showmsg3); else / 接收到的是別人發(fā)給我的消息(悄悄話)insert(myMsgArea, + showmsg1 + 對您說: + showmsg3);putout(si.txt, + showmsg1 + 對您說: + showmsg3); elseinsert(commonArea, inmsg); catch (Exception ee)ee.printStackTrace();insert(myMsgArea, 與服務器中斷,請重新登錄!);in = null;out = null;return;/ /*發(fā)送文件線程*/class sendfilethread extends Thread/ ActionListenerkhhkh壓郁sendfilethread()/ 構造函數(shù)public void run()File file = new File(sendfilename);FileInputStream fos = null;tryfos = new FileInputStream(file); catch (IOException e1)System.out.pr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論