版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
利用JAVA實現(xiàn)簡單聊天室.設計思路Java是一種簡單的,面向對象的,分布式的,解釋的,鍵壯的,安全的,結構中立的,可移植的,性能很優(yōu)異的,多線程的,動態(tài)的語言。而且,Java很小,整個解釋器只需215K的RAM。因此運用JAVA程序編寫聊天室,實現(xiàn)簡單聊天功能。程序實現(xiàn)了聊天室的基本功能,其中有:(1)啟動服務器:實現(xiàn)網(wǎng)絡的連接,為注冊進入聊天室做準備。(2)注冊登陸界面:填寫基本信息如姓名等,可以供多人進入實現(xiàn)多人聊天功能。(3)發(fā)送信息:為用戶發(fā)送信息提供平臺。(4)離開界面:使用戶退出聊天室。(5)關閉服務器:斷開與網(wǎng)絡的連接,徹底退出聊天室。.設計方法在設計簡單聊天室時,需要編寫5個Java源文件:、、、、。程序功能圖及程序相關說明(1)主功能框圖(2)聊天室基本功能表序號文件名主要功能1ServerHost被服務器調用2Client顯示聊天室3ClientUser被客戶端調用4LogIn注冊登陸5Objecting連接服務器6Server啟動服務器4.程序代碼是說明程序中引入的包:packageChat;import.*;import.*;import.*;import.*;import.*;(1)服務器端代碼中用戶自定義類:類名:Server作用:服務器啟動繼承的接口名:ActionListenerpublicclassServerimplementsActionListener{定義的對象:count計結果與分析5個Java源文件:、、、、。分別編譯這5個Java源文件,或運行“javac*.java”命令,編譯全部的源文件,然后運行連接服務器的主類,即運行Server類。連接完畢后在運行注冊的主函數(shù)類,即運行LogIn類。首先運行啟動服務器的文件,運行結果如下:圖1服務器啟動成功結果分析:在進入聊天室之前必須先啟動服務器,實現(xiàn)服務器端的連接為用戶的登錄打開“窗口”。其次運行用戶注冊登錄文件,運行結果如下:
圖2用戶登陸界面結果分析:添加用戶信息,輸入用戶姓名,為用戶進入聊天室提供關口。并且利用以下語句實現(xiàn)如圖3的界面JLabeltitle=newJLabel("免注冊版");JLabelname=newJLabel("輸入姓名");JButtonsend=newJButton("進入");添加注冊信息后點擊“進入”按鈕,結果如圖3圖3顯示用戶已登錄結果分析:當服務器啟動用戶完成信息注冊時,在服務期端的界面中會顯示當前在線的人員,并且提示客戶已連接到服務器,可以進行聊天行為。
圖4顯示聊天內容結果分析:多個用戶進入聊天室之后,用戶可以在發(fā)送信息的文本框里填寫自己的內容,點擊“發(fā)送信息”按鈕,信息會出現(xiàn)在上方的區(qū)域內,在界面的右方還可以顯示在線人員的姓名,另在線用戶可以知道有誰在聊天室內。聊天結束點擊“離開”按鈕,結果如圖5所示:mJK用■壽匚百|耳|后止眠第■能啟動眼笛器接收用戶飛飛的連接眠第■器扭收用戶豆豆的連接服第■器接收用戶大寶的連接豆豆由弁立寶國開飛飛離井I圖5離開聊天室點擊服務器中的“停止”按鈕,結果如圖6.圖7.所示:圖6點擊“停止”按鈕£賑如部匚][□)|5c|后碼I版券器啟動服務智厚報也用尸飛飛的匡抵目員岳昌缶探收用尸豆豆白勺生接眼綃即拯收壓戶大主時]注拉豆豆際開次妄離開飛飛離開滋送月員結5S工田泗A.圖7終止與服務器端連接結果分析:用戶聊天結束點擊“離開”按鈕之后會在服務器端的界面上自動顯示該用戶已離開,提時、示服務器執(zhí)行相關行為。離開界面:使用戶退出聊天室;關閉服務器:斷開與網(wǎng)絡的連接,徹底退出聊天室。6.語句實現(xiàn)(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();("系統(tǒng)消息服務器馬上關閉?。。。。?!請馬上關閉客戶端");}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();}}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){();}}}packageChat;import.*;import.*;import.*;import.*;importclassClientimplementsActionListener{ClientSocketcs;Stringname;Socketsocket;JFrameframe;JTextAreaarea=newJTextArea(30,30);JTextFieldtext=newJTextField(30);JButtonsend:newJButton("發(fā)送消息");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=();String。0玳6玳=(£好山6,"輸入向”+92山6+”發(fā)送的私聊信息","私聊",;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=();BufferedReaderBsoc_in=newBufferedReader(newInputStreamReader()));Strings=();(soc,s);Se
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 合同協(xié)議網(wǎng)絡廣告協(xié)議
- 借款延期合同
- 借款合同借款合同范文
- 《深入解析HTTP協(xié)議》課件
- 金錢與期刊:媒體禁制令及一稿多投禁制
- 區(qū)塊鏈共識機制與改進算法研究進展
- 生長因子促進膝軟骨再生:研究熱點的文獻計量學分析
- 基于機器視覺的智能車系統(tǒng)設計
- 考慮失效相關性的無人機飛控系統(tǒng)可靠性分配與預計
- 基于藍牙的LBS系統(tǒng)攻擊檢測方法研究
- 2025年蛇年年度營銷日歷營銷建議【2025營銷日歷】
- (一模)寧波市2024學年第一學期高考模擬考試 數(shù)學試卷(含答案)
- 父母贈與子女農(nóng)村土地協(xié)議書范本
- 集團母子公司協(xié)議書
- 中醫(yī)病證診斷療效標準
- 南安市第三次全國文物普查不可移動文物-各鄉(xiāng)鎮(zhèn)、街道分布情況登記清單(表五)
- ITSMS-D-038 問題記錄表范本
- 第1課+古代亞非(教學設計)【中職專用】《世界歷史》(高教版2023基礎模塊)
- 物業(yè)客服管家的培訓課件
- 2024年房地產(chǎn)行業(yè)的樓市調控政策解讀培訓
- 《統(tǒng)計學-基于Python》 課件全套 第1-11章 數(shù)據(jù)與Python語言-時間序列分析和預測
評論
0/150
提交評論