




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、. . . . Java項目設計聊天室設計設計人:XXX指導老師:XXX設計時間:2005-4-17到2005-4-24目錄第一部分 項目概況 1.項目說明.42.項目背景.4第二部分 項目計劃1. 項目目標 42. 項目圍 43. 項目策略 44. 項目開發(fā)周期 45. 每項任務所需技能 56. 所采用的工具和技術清單5第三部分 項目設計1 需求分析.52 軟件功能.53 概要設計.64 詳細設計.6第四部分 項目測試1. 測試階段2程序界面.73程序運行的條件 4參考的資料第五部分 活動總結(jié)第一部分 項目概況1.項目說明項目名稱:JAVA聊天室系統(tǒng)???戶:一般大眾。2、項目背景NIIT的
2、課程重在對實際動手能力的培養(yǎng)。通過一段時間JAVA課程的學習,需要對所學的知識作一個綜合的運用。由于JAVA強大的網(wǎng)絡編程能力,需要寫一個簡單的聊天室程序的項目,主要能實現(xiàn)在網(wǎng)絡上的多線程間的通訊。項目的目的在于鞏固前段時間所學的知識以與鍛煉小組成員之間團隊合作的能力。第二部分 項目計劃1. 項目目標(1),允許新用戶注冊,同時開設。(2),登陸后在用戶列表框顯示所有的用戶。(3),允許用戶進行在線的公聊和私聊。(4),把客戶端的界面放在Applet上面,在聊天時可以對面版進行一些簡單設置。2. 項目圍本軟件適用于一般大眾。3. 項目策略通過可視化編程的JAVA程序的方式進行實現(xiàn)。 通過增量開
3、發(fā)的方法進行此項目的設計與開發(fā)。使用套節(jié)字進行客戶與服務端的通訊。4項目開發(fā)周期第一天 需求分析與初步設計階段(決定每個功能所需的類與數(shù)據(jù)類型)第二天 根據(jù)相應功能設計出各類所用到的方法與數(shù)據(jù)類型第三天第五天 具體實現(xiàn)各個類中的各個方法第六天 對各個界面和服務器進行調(diào)整與測試完善工作5. 每項任務所需技能服務端與客戶端的連接:網(wǎng)絡編程中的協(xié)議與數(shù)據(jù)流,異常處理,多線程。程序界面設計:GUI圖形用戶組件,布局管理,事件監(jiān)聽。代碼實現(xiàn):JAVA與數(shù)據(jù)庫編程。6. 所采用的工具開發(fā)工具:Microsoft JAVALE(jdk1.4)+ JCreator LE開發(fā)環(huán)境:windowsXP+個人電腦第
4、三部分 項目設計1.需求分析隨著經(jīng)濟的日益發(fā)展,網(wǎng)絡的觀念深入人心。人們越來越需要通過網(wǎng)絡高效簡捷的交流,聊天室就是其中的一個工具。2.軟件主要功能(1),允許新用戶注冊,同時開設。(2),登陸后在用戶列表框顯示所有的用戶。(3),允許用戶進行在線的公聊和私聊。(4),把客戶端的界面放在Applet上面,在聊天時可以對面版進行一些簡單設置。程序中要用到的類1、 客戶端 2、服務器端3概要設計聊天室的程序至少需要建立兩個程序:一個服務器端,負責監(jiān)聽客戶端發(fā)送過來的請求,一但監(jiān)聽到客戶端的請求,先判斷它是登陸信息還是聊天信息,若是登陸信息,那么允許客戶端登陸,并把登陸的客戶名返回給客戶端顯示在用戶
5、在線列表框。如果是聊天信息,則把它群發(fā)給每個客戶端。服務器端不對聊天的信息作公聊和私聊的判斷,由客戶端收到消息后判斷是否是發(fā)給自己的信息,以與是否顯示在聊天面板上面。客戶端的程序需要運用圖形化編程做出一個界面。我們小組所做的界面是在聊天室界面的左上方為一個在線用戶列表框,列表框的下面有用戶登陸的文本域和密碼輸入框。右邊是聊天面板,聊天面板的下面是輸入聊天信息的文本域,以與發(fā)送和清空按紐。當輸入用戶名和密碼登陸后,在用戶列表框會顯示當前在線的所有的用戶并且在聊天面板上顯示某用戶進入了聊天室。然后屏蔽掉登陸和密碼輸入框。在信息輸入框中輸入聊天信息后點擊發(fā)送,在所有客戶端的聊天面板上會看到你發(fā)送的聊
6、天信息。在用戶列表框選擇某個用戶,勾選私聊復選框然后發(fā)送消息,這時只有被你選擇的客戶端能看到你發(fā)送的信息。4 詳細設計服務器端代碼:/聊天室服務器端程序import java.io.*;import .*;import java.util.*;public class Homework implements RunnableThread thread;static Vector vector;ServerSocket server;public Homework()tryserver=new ServerSocket(8000);System.out.println(服務器已啟動);catch
7、(IOException e)vector=new Vector(100);thread=new Thread(this);thread.start();public void run()trywhile(true)Socket socket=server.accept();PThread pt=new PThread(this,socket);pt.setPriority(Thread.MAX_PRIORITY);pt.start();vector.addElement(pt);catch(IOException r)public static void main(String args)n
8、ew Homework();synchronized public void broadcast(String name)PThread cg;String user;if(name.startsWith(login)for(int i=0;ivector.size();i+)cg=(PThread)vector.elementAt(i);trycg.dout.writeUTF(name+高興地走進了聊天室.ok!);catch(IOException t) else if(name.startsWith(public) System.out.println(name.substring(6)
9、; for(int i=0;ivector.size();i+) cg=(PThread)vector.elementAt(i); try cg.dout.writeUTF(name); catch(IOException a) else if(name.startsWith(private) System.out.println(name.substring(7); for(int i=0;ivector.size();i+) cg=(PThread)vector.elementAt(i); try cg.dout.writeUTF(name); catch(IOException f) /
10、class PThread extends Threadprotected Socket socket;protected Homework home;protected DataInputStream din;protected DataOutputStream dout;String message,message1;PThread cg;public PThread(Homework home,Socket socket)this.socket=socket;this.home=home;trydin=new DataInputStream(socket.getInputStream()
11、;dout=new DataOutputStream(socket.getOutputStream();catch(IOException w)public void run()trywhile(true)message1=din.readUTF();if(message1.startsWith(login)message=message1.substring(5,message1.indexOf( )+1);if(true)for(int i=0;ihome.vector.size();i+)cg=(PThread)home.vector.elementAt(i);dout.writeUTF
12、(list+cg.message);home.broadcast(login+message);System.out.println(message+登陸了); if(message1.startsWith(public) message=message1.substring(6,message1.indexOf(); if(true) for(int i=0;ihome.vector.size();i+) cg=(PThread)home.vector.elementAt(i); /dout.writeUTF(public+message); home.broadcast(public+me
13、ssage); if(message1.startsWith(private) message=message1.substring(7,message1.indexOf(); if(true) for(int i=0;ihome.vector.size();i+) cg=(PThread)home.vector.elementAt(i); /dout.writeUTF(private+message); home.broadcast(private+message); catch(IOException e)客戶端代碼:/聊天室客戶端程序import java.awt.*;import ja
14、va.applet.Applet;import java.applet.AudioClip;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.util.Date;import .*; public class Homework_client1 extends Applet implements RunnableList UesrNameList; /顯示用戶名的列表;TextArea Text_Area; /顯示用戶發(fā)言的文本域;JLabel UserName; /顯示用戶;JLabel UNam
15、e; /用戶面板;JLabel PassWord; /顯示登陸口令;TextField PWord; /登陸口令文本框;TextField UN; /用戶文本框;JTextField forField; /說文本框;JTextField Sayfield; /發(fā)送文本框;JLabel Vslabel; /對面板;JLabel Saylabel; /說面板;JLabel Frontlabel; /前景色面板;JLabel Backlabel; /背景色面板;JLabel Stylelabel; /字體面板;JLabel Wordlabel; /字號面板;utton Enterbutton; /進
16、入按紐; utton Quitbutton; /退出按紐;utton Sendbutton; /發(fā)送按紐;utton Canecelbutton; /刷新按紐; Checkbox Quient; /私聊復選框;Choice C_style; /字體選擇框;Choice C_word; /字號選擇框;Choice C_front; /前景色選擇框;Choice C_back; /背景色選擇框;Choice C_music; /音樂選擇框;AudioClip Sound; /音樂播放器;JMenuItem Play; /play;JMenuItem Loop; /loop;JMenuItem St
17、op; /stop; JMenu Start; /播放器菜單;JMenuBar SoundBar; /加載播放菜單; Socket socket; /客戶端;Thread Cthread=new Thread(this);DataInputStream din; /數(shù)據(jù)輸入流;DataOutputStream dout; /數(shù)據(jù)輸出流;/*面板的編寫*/public void init()/*定義左邊的組件(用戶名、用戶列表、登陸按紐等)*/*將左邊組件進行網(wǎng)格包布局,并添加到面板*/Thread Cthread; /創(chuàng)建一個和客戶端進行通信的線程;UesrNameList=new List(
18、19);UesrNameList.setBackground(Color.WHITE);UesrNameList.setForeground(new Color(255,0,255);UesrNameList.setFont(new Font(cg,Font.BOLD,14);UserName=new JLabel(用 戶 姓 名);UserName.setFont(new Font(cg,Font.ITALIC,14);UName=new JLabel(姓 名);UN=new TextField(11);UN.setFont(new Font(cg,Font.BOLD,13);PassWor
19、d=new JLabel(密 碼);PWord=new TextField(11);PWord.setEchoChar(*);Enterbutton=new utton(進入);Enterbutton.setBackground(new Color(136,193,255);Enterbutton.setToolTipText(點此連接服務器);Quitbutton=new utton(退出);Quitbutton.setToolTipText(點此離開聊天室);Quitbutton.setEnabled(false);Quitbutton.setBackground(new Color(13
20、6,193,255);C_music=new Choice(); /初始化音樂控件;C_music.add(遇見.mid);C_music.add(十年.mid);SoundBar=new JMenuBar();SoundBar.setBackground(new Color(136,193,255);Start=new JMenu(音樂);Start.setBackground(new Color(136,193,255);Play=new JMenuItem(播放);Loop=new JMenuItem(循環(huán));Stop=new JMenuItem(停止);Start.add(Play);
21、Start.add(Loop);Start.add(Stop);SoundBar.add(Start);Sound=getAudioClip(getCodeBase(),遇見.mid);JPanel panel_0=new JPanel();panel_0.setBackground(new Color(136,193,255);GridBagLayout gb_0=new GridBagLayout();GridBagConstraints gc_0=new GridBagConstraints();gc_0.fill=GridBagConstraints.NONE;gc_0.weightx
22、=0.0;gc_0.weighty=0.0;gb_0.setConstraints(C_music,gc_0);gb_0.setConstraints(SoundBar,gc_0);panel_0.setLayout(gb_0);panel_0.add(C_music);panel_0.add(SoundBar);JPanel panel_1=new JPanel();panel_1.setBackground(new Color(136,193,255);panel_1.add(UName);panel_1.add(UN);JPanel panel_2=new JPanel();panel_
23、2.setBackground(new Color(136,193,255);panel_2.add(PassWord);panel_2.add(PWord);JPanel panel_3=new JPanel();panel_3.setBackground(new Color(136,193,255);panel_3.add(Enterbutton);panel_3.add(Quitbutton);JPanel NO1panel=new JPanel();NO1panel.setBackground(new Color(136,193,255);GridBagLayout gb_1=new
24、GridBagLayout();GridBagConstraints gc_1=new GridBagConstraints();gc_1.anchor=GridBagConstraints.NORTHWEST;gc_1.gridx=0;gc_1.gridy=0;gb_1.setConstraints(panel_0,gc_1);gc_1.anchor=GridBagConstraints.NORTHWEST;gc_1.gridx=0;gc_1.gridy=1;gb_1.setConstraints(UserName,gc_1);gc_1.anchor=GridBagConstraints.N
25、ORTHWEST;gc_1.gridx=0;gc_1.gridy=2;gb_1.setConstraints(UesrNameList,gc_1);gc_1.anchor=GridBagConstraints.NORTHWEST;gc_1.gridx=0;gc_1.gridy=3;gb_1.setConstraints(panel_1,gc_1);gc_1.anchor=GridBagConstraints.NORTHWEST;gc_1.gridx=0;gc_1.gridy=4;gb_1.setConstraints(panel_2,gc_1);gc_1.anchor=GridBagConst
26、raints.NORTHWEST;gc_1.gridx=0;gc_1.gridy=5;gb_1.setConstraints(panel_3,gc_1);NO1panel.setLayout(gb_1);NO1panel.add(panel_0);NO1panel.add(UserName);NO1panel.add(UesrNameList);NO1panel.add(panel_1);NO1panel.add(panel_2);NO1panel.add(panel_3);/*定義右下方中間的組件*/Quient=new Checkbox(私聊,false);Quient.setBackgr
27、ound(new Color(136,193,255);Quient.setSize(1,1);Vslabel=new JLabel(對);Vslabel.setSize(1,1);forField=new JTextField(7);forField.setEditable(false);Saylabel=new JLabel(說);Saylabel.setSize(1,1);Sayfield=new JTextField(28);Sayfield.setFont(new Font(cg,Font.ROMAN_BASELINE,14);Sayfield.setEditable(false);
28、Sendbutton=new utton(發(fā)送);Sendbutton.setEnabled(false);Sendbutton.setBackground(new Color(136,193,255);/*定義右下方下層組件*/Stylelabel=new JLabel(字體);Stylelabel.setSize(1,1);Wordlabel=new JLabel(表情);Wordlabel.setSize(1,1);Frontlabel=new JLabel(前景色);Frontlabel.setSize(1,1);Backlabel=new JLabel(背景色);Backlabel.
29、setSize(1,1);Canecelbutton=new utton(刷新);Canecelbutton.setBackground(new Color(136,193,255);C_style=new Choice(); /字體選擇框C_style.addItem(BLOD);C_style.addItem(ITALIC);C_style.addItem(PLAIN);C_word=new Choice(); /表情選擇框C_word.setSize(2,1);C_word.addItem(無);C_word.addItem(微笑);C_word.addItem(憤怒);C_word.a
30、ddItem(激動);C_front=new Choice(); /前景色選擇框C_front.addItem(RED);C_front.addItem(ORANGE);C_front.addItem(BLUE);C_front.addItem(BLACK);C_back=new Choice(); /背景色選擇框C_back.addItem(RED);C_back.addItem(ORANGE);C_back.addItem(BLUE);C_back.addItem(BLACK);/*將右下方組件進行網(wǎng)格包布局,并添加到面板*/JPanel panel_4=new JPanel(); pan
31、el_4.setBackground(new Color(136,193,255);GridBagLayout gb_2=new GridBagLayout();GridBagConstraints gc_2=new GridBagConstraints();gc_2.fill=GridBagConstraints.BOTH;gc_2.weightx=1.0;gc_2.weighty=1.0;gb_2.setConstraints(Quient,gc_2);gb_2.setConstraints(Vslabel,gc_2);gc_2.fill=GridBagConstraints.REMAIN
32、DER;gc_2.weightx=1.0;gc_2.weighty=1.0;gb_2.setConstraints(forField,gc_2);gb_2.setConstraints(Saylabel,gc_2);gb_2.setConstraints(Sayfield,gc_2);gc_2.fill=GridBagConstraints.RELATIVE;gc_2.weightx=1.0;gc_2.weighty=1.0;gb_2.setConstraints(Sendbutton,gc_2);panel_4.setLayout(gb_2);panel_4.add(Quient);pane
33、l_4.add(Vslabel);panel_4.add(forField);panel_4.add(Saylabel);panel_4.add(Sayfield);panel_4.add(Sendbutton);Text_Area=new TextArea(10,10);Text_Area.setEditable(false);Text_Area.setFont(new Font(cg,Font.PLAIN,16);Text_Area.setText(tttt* 歡迎來到聊天室 *);Text_Area.setForeground(new Color(136,193,255);JScroll
34、Pane scroll=new JScrollPane(Text_Area);/將AREA添加到滾動面板/*將最右下方組件進行網(wǎng)格包布局,并添加到面板*/JPanel panel_5=new JPanel(); panel_5.setBackground(new Color(136,193,255);GridBagLayout gb_3=new GridBagLayout();GridBagConstraints gc_3=new GridBagConstraints();gc_3.fill=GridBagConstraints.LINE_START;gc_3.weightx=1.0;gc_3
35、.weighty=1.0;gb_3.setConstraints(Stylelabel,gc_3);gc_3.fill=GridBagConstraints.RELATIVE;gc_3.weightx=1.0;gc_3.weighty=1.0;gb_3.setConstraints(C_style,gc_3);gb_3.setConstraints(Wordlabel,gc_3);gb_3.setConstraints(C_word,gc_3);gb_3.setConstraints(Frontlabel,gc_3);gb_3.setConstraints(C_front,gc_3);gb_3
36、.setConstraints(Backlabel,gc_3);gb_3.setConstraints(C_back,gc_3);gc_3.fill=GridBagConstraints.REMAINDER;gb_3.setConstraints(Canecelbutton,gc_3);panel_5.setLayout(gb_3);panel_5.add(Stylelabel);panel_5.add(C_style);panel_5.add(Wordlabel);panel_5.add(C_word);panel_5.add(Frontlabel);panel_5.add(C_front)
37、;panel_5.add(Backlabel);panel_5.add(C_back);panel_5.add(Canecelbutton);JPanel panel_6=new JPanel();panel_6.setLayout(new GridLayout(2,1);panel_6.add(panel_4);panel_6.add(panel_5);/*右邊組件實現(xiàn)滾動*/JSplitPane sp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,scroll,panel_6);sp.setDividerLocation(400);sp.setDivid
38、erSize(10);/*左右組件進行連接并實現(xiàn)滾動*/JSplitPane sp1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,NO1panel,sp);sp1.setEnabled(false);sp1.setDividerLocation(158);sp1.setPreferredSize(this.getSize();sp1.setDividerSize(10);/*加載至Applet*/JPanel panel_7=new JPanel();GridBagLayout gb_4=new GridBagLayout();GridBagConst
39、raints gc_4=new GridBagConstraints();gc_4.fill=GridBagConstraints.BOTH;gc_4.weightx=1.0;gc_4.weighty=1.0;gb_4.setConstraints(sp1,gc_4);panel_7.setLayout(gb_4);panel_7.add(sp1);panel_7.setSize(this.getMaximumSize();panel_7.setBorder(BorderFactory.createTitledBorder(ITEM2 CharRoom);this.setLayout(new
40、BorderLayout();add(panel_7,BorderLayout.CENTER);/*注冊事件*/Enterbutton.addActionListener(new Enter();Sendbutton.addActionListener(new Send();Canecelbutton.addActionListener(new Cancel();Quitbutton.addActionListener(new Quit();UesrNameList.addMouseListener(new UNL();C_style.addItemListener(new C_s();C_f
41、ront.addItemListener(new C_f();C_back.addItemListener(new C_b();C_music.addItemListener(new C_m();Play.addActionListener(new PL();Loop.addActionListener(new LO();Stop.addActionListener(new ST(); /*用戶登陸檢測、按紐激活以與系統(tǒng)登陸*/ class Enter implements ActionListenerpublic void actionPerformed(ActionEvent e) if(
42、e.getSource().equals(Enterbutton)&(!(UN.getText().equalsIgnoreCase() &(!(PWord.getText().equalsIgnoreCase() Quitbutton.setEnabled(true); Sendbutton.setEnabled(true); Sayfield.setEditable(true); Enterbutton.setEnabled(false); PWord.setEnabled(false); UN.setEnabled(false); try /*和服務器建立連接*/ socket=new
43、Socket(127.0.0.1,8000); Text_Area.append(nn網(wǎng)絡連接成功! + n); din=new DataInputStream(socket.getInputStream(); dout=new DataOutputStream(socket.getOutputStream(); String message=login+UN.getText()+ ; UesrNameList.add(所有人); dout.writeUTF(message); /向服務器寫入用戶名; Cthread.start(); catch(IOException q) Text_Are
44、a.append(n連接錯誤); catch(Exception ) Text_Area.append(n無法連接); else JOptionPane.showConfirmDialog(null,new String(用戶名或密碼不能為空),錯誤,JOptionPane.ERROR_MESSAGE); /*事件處理*/class Send implements ActionListenerpublic void actionPerformed(ActionEvent ee) if(ee.getSource().equals(Sendbutton)&(!(Sayfield.getText()
45、.equalsIgnoreCase() if(Quient.getState()=true) if(forField.getText().equalsIgnoreCase(所有人) Text_Area.append(請選擇一個具體用戶n); else try dout.writeUTF(private+UN.getText()+悄悄的+對.+forField.getText()+C_word.getSelectedItem()+的說+:+Sayfield.getText()+); catch(IOException ss) Sayfield.setText(); else if(Quient.
46、getState()=false) if(!(forField.getText().equalsIgnoreCase() try dout.writeUTF(public+UN.getText()+對+forField.getText()+C_word.getSelectedItem()+的說+:+Sayfield.getText()+); catch(IOException dd) Sayfield.setText(); else try dout.writeUTF(public+UN.getText()+對所有人+C_word.getSelectedItem()+的說+:+Sayfield.getText()+); catch(IOException dd) Sayfield.setText(); else JOptionPane.showConfirmDialog(null,new String(發(fā)言不能為空),錯誤,JOptionPane.ERROR_MESSAGE); class Quit implements ActionListenerpublic void actionPerformed(ActionEvent e)Text_Area.append(n移除線程待完成n);class Cancel implements Acti
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 通信工程光纖傳輸系統(tǒng)試題集
- 辦公室接待來賓登記表
- 攝影工作室拍攝風格更改免責協(xié)議
- 體育場館運營與維護服務合同
- 治療協(xié)議服務合同
- 黑龍江省佳木斯市富錦市2024-2025學年九年級上學期期末生物學試題(含答案)
- 財務會計準則下的財務報表編制試題
- 滑雪培訓服務合同
- 幼兒園小班故事表演活動解讀
- 公司新年營銷策略規(guī)劃與執(zhí)行方案設計
- 2024.8.1十七個崗位安全操作規(guī)程手冊(值得借鑒)
- 電影《白日夢想家》課件
- 深度學習及自動駕駛應用 課件 第1章 汽車自動駕駛技術概述
- 汽車4S點隱患排查治理體系(清單及排查表)
- UV數(shù)碼噴印墨水市場分析
- 記憶有方 過目不忘 課件
- 無人機應用與基礎操控入門課件
- 2024年全國職業(yè)院校技能大賽中職組(短視頻制作賽項)考試題庫-下(多選、判斷題)
- 口腔病歷管理制度內(nèi)容
- 三一燈塔工廠解決方案
- 四川省會計師事務所服務收費標準
評論
0/150
提交評論