JAVA聊天室參考實現(xiàn)_第1頁
JAVA聊天室參考實現(xiàn)_第2頁
JAVA聊天室參考實現(xiàn)_第3頁
JAVA聊天室參考實現(xiàn)_第4頁
JAVA聊天室參考實現(xiàn)_第5頁
已閱讀5頁,還剩40頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

基于JAVA的談天室設計與實現(xiàn)設計目的:信息技術的高速進展給人類生產,生活帶來了廣泛而深遠的影響?!吧暇W(wǎng)沖浪”、“數(shù)碼相機”“PDA”、“彩屏手機”、“藍牙技術"等新產品,新概念層出不窮,不斷沖擊著人們的視聽。這些高科技成果為人們帶來便捷、快樂的同時,也促進了人機交互技術的進展。人機交互是關于設計、評價和實現(xiàn)供人們使用的交互式計算機系統(tǒng),并圍繞相關的主要現(xiàn)象進行討論的科學。它主要討論人與計算機之間的信息交換,涉及計算機科學,心理學,認知科學等諸多學科,是信息技術的一個重要組成部分,并將連續(xù)對信息技術的進展產生巨大影響。談天室是一項應用廣泛,并且有用性強的一個格外典型的人機交互系統(tǒng).在信息世界里,談天工具的使用是格外頻繁的,如MSN、QQ、校內通,之類的談天工具很多人都正在使用。此次設計的談天室系統(tǒng)雖然其功能簡潔,但是在開發(fā)技術和過程中,能夠充分學習和使用JAVA的技術,進一步培育同學面對對象程序設計的思想,加深對面對對象程序設計四大特性的理解。二、需求分析人們日常生活中越來越多地使用談天室這項應用來為自己的工作和學習服務。一個操作簡潔,界面友好、運行穩(wěn)定的談天室對于小型局域網(wǎng)的用戶可以起到很好的溝通作用。所謂談天室就是一個可以讓很多用戶同時相互通信的網(wǎng)上園地,個人用戶可以通過談天室將自己融入整個主流社會中。依據(jù)目前使用談天室的用戶需求顯示,用戶對于談天室主要有以下幾點要求:(1)、談天功能:可以實時接收和發(fā)送信息,并能支持公聊和私聊的的方式,能夠查看談天記錄,談天過程中能對字體、表情等進行設定等。(2)、用戶功能:可以隨時注冊和登錄及注銷,并能選擇性加好友和刪除好友以及查看對方的開放信息,還能看到對方的登錄狀態(tài)。(3)、文件傳輸功能:用戶可以共享資源,能發(fā)送及接收文件。(4)、保密功能:用戶可以設定自己的保密狀態(tài),使某些信息對于其他用戶是不行見的。(5)、系統(tǒng)穩(wěn)定:客戶端與服務端的連接穩(wěn)定,不消滅或者少消滅連接不上的情況。該簡易談天室是為很多喜愛談天的用戶開發(fā)設計的,和很多大型的網(wǎng)絡談天室相比該小談天室功能比較簡潔,但能實現(xiàn)基本的談天功能,點對多、點對點談天和基本的用戶功能,登錄、注銷、查看在線用戶等。三、軟件功能描述(1)、用戶設置:功能描述:用戶自定義自己的登錄名,成功設置后,服務端能接收到新設置的用戶名;(2)、登錄功能:功能描述:在服務端開啟的情況下,用戶可以以自己自定義或者默認的登錄名和登錄地址端口登錄到客戶端,并能在服務器端顯示登錄狀態(tài);(3)、公聊:功能描述:用戶在談天室中可以選擇與“全部人”談天,談天室中的其他在線用戶可以正常接收并正確顯示公聊消息。(4)、私聊:功能描述:用戶可以選擇與談天室的在線用戶列表中的其他用戶談天。雙方的談天記錄對于談天室的其他用戶是不行見的。(5)、談天心情:功能描述:用戶在談天的過程中可以選擇相應的心情來表達自己的情感.(6)、系統(tǒng)消息:功能描述:服務端正常運行的過程中,在談天室的每一個用戶都能接收服務器端發(fā)送的信息提示信息。(7)、用戶的狀態(tài)。功能描述:每一個客戶端用戶都可以實時看到目前其他在線用戶的狀態(tài)。(8)、用戶注銷:功能描述:用戶可以注銷自己的賬號,并重新登記登錄名和地址。(9)、用戶退出:功能描述:用戶可以在正常情況下退出談天室。(10)、服務端掌握:功能描述:只有在服務端開啟的情況下,用戶才能登陸成功和談天;用戶設置的地址端口也必須與服務端的偵聽端口全都,否則不能登錄.四:主要模塊描述及流程圖模塊描述本談天室分為談天室服務端和談天室客戶端,服務端與客戶端之間通過套接口Socket(TCP)連接.在java中使用套接口相當簡潔,JavaAPI為處理套接口的通信供應了一個類ja.Socket.,使得編寫網(wǎng)絡應用程序相對容易。服務器采納多線程以滿意多用戶的懇求,通過JDBC與后臺數(shù)據(jù)庫連接,并通過創(chuàng)建一個ServerSocket對象來監(jiān)聽來自客戶的連接懇求,默認IP地址為:127。0.0.1默認端口為8888。(1)談天室服務端模塊圖:聊天室服務端聊天室服務端發(fā)送系統(tǒng)消息退出服務器消息顯示停止服務啟動服務發(fā)送系統(tǒng)消息退出服務器消息顯示停止服務啟動服務端口服務端口服務:輸入要偵聽的端口,默認為8888;啟動服務:啟動服務器,并開頭在設置的端口中偵聽,客戶端用戶可以登錄并開頭談天;停止服務:關閉服務器,偵聽結束.客戶端用戶不能再談天;發(fā)送系統(tǒng)消息:服務器端給全部人或者個別用戶發(fā)送消息.消息顯示:可以顯示全部用戶的談天記錄,和上線、下線信息。退出服務器:退出程序,并停止服務;(2)談天室客戶端模塊圖:聊天室客戶端用戶設置聊天室客戶端用戶設置用戶注銷用戶注銷發(fā)送/接收聊天消息退出用戶登錄發(fā)送/接收聊天消息退出用戶登錄連接設置用戶設置:用戶可以設置自己的用戶名進行談天;連接設置:用戶可以設置自己的服務器的IP地址和端口號,默認的連接地址為:127.0。0.1:8888;用戶登錄:只能在談天室服務端已開啟的情況下,用戶才能登錄,用戶登錄之后才能開頭談天,登錄后不能再修改用戶設置和連接設置;用戶注銷:退出談天,可以連續(xù)修改用戶設置和連接設置;發(fā)送和接收談天信息:用戶可以給全部人或者個別用戶發(fā)送信息,并能看到其他用戶發(fā)送給自己或全部人的消息;退出:關閉全部談天并退出客戶端。流程圖聊天室服務器啟動服務端口服務設置偵聽端口聊天室服務器啟動服務端口服務默認端口:8888聊天登錄用戶設置聊天室客戶端聊天登錄用戶設置聊天室客戶端設置用戶名默認連接地址或用戶名連接設置退出設置IP地址和端口連接設置退出五.主要類UML關系圖:ActionListenerChatServer-port:publicstaticinActionListenerChatServer-port:publicstaticin-serverSocket:ServerSocket-messageShow:JTextArea-userLinkList:UserLinkList-showStatus:JTextField+ChatServer():public+init():publicvoid+startService():publicvoid+stopService():publicvoid+sendStopToAll():publicvoid+sendMsgToAll(String):publicvoid+sendSystemMessage():publicvoidJFrameThreadServerListen-server:ServerSocket-userLinkList:UserLinkList -isStop:publicBoolean+ServerListen(ServerSocket,JComboBox,JTextArea,JTextField,UserLinkList)ServerReceive-textarea:JTextArea-userLinkList:UserLinkList -isStop:publicBoolean+ServerListen(ServerSocket,JComboBox,JTextArea,JTextField,UserLinkList)ServerListenServerListen-server:ServerSocket-userLinkList:UserLinkList -isStop:publicBoolean+ServerListen(ServerSocket,JComboBox,JTextArea,JTextField,UserLinkList)1、談天室服務器端設計:談天室服務端主要包括7個文件,它們的功能如下:(1)ChatServer.java:包含名為ChatServer的public類,其主要功能為定義服務器端的界面,添加大事偵聽與大事處理。調用ServerListen類來實現(xiàn)服務端用戶上線與下線的偵聽,調用ServerReceive類來實現(xiàn)服務器端的消息的收發(fā)。(2)ServerListen.java:該類實現(xiàn)服務端用戶上線與下線的偵聽。該類對用戶上線下線的偵聽是通過調用用戶鏈表類(UserLinkList)來實現(xiàn)的,當用戶上線與下線情況發(fā)生變化時,該類會對主類的界面進行相應的修改。(3)ServerReceive.java:該類是實現(xiàn)服務器端的消息的收發(fā)的類.該類分別定義了向某用戶及全部人發(fā)送消息的方法,發(fā)送的消息會顯示在主界面類的界面上。(4)PortConf.java:該類繼承自JDialog,是用戶對服務器端偵聽端口進行修改配置的類.(5)Node。java:用戶鏈表的節(jié)點類,定義了鏈表中的用戶。該類與前面所講的鏈表節(jié)點Node類的功能相當。(6)UserLinkList.java:用戶鏈表節(jié)點的簡略實現(xiàn)類.該類通過構造函數(shù)構造用戶鏈表,定義了添加用戶,刪除用戶、返回用戶數(shù)、依據(jù)用戶名查找用戶、依據(jù)索引查找用戶這5個方法。(7)Help.java:服務端程序的幫助類。Thread談天室客戶端設計:ThreadActionListenerActionListenerChatClient-port:publicstaticin-serverSocket:ServerSocket-messageShow:JTextArea-userLinkList:UserLinkList-showStatus:JTextField+ChatClient():public+init():publicvoid+Connect():publicvoid+DisConnect():publicvoid+SendMessage():publicvoidJFrameClientReceiveClientReceive-socket:Socket-output:ObjectOutputStream-input:ObjectInputStream+ClientReceive(Socket,ObjectOutputStream,ObjectInputStream,JComboBox,JTextArea,JTextField)談天室客戶端主要包括5個文件,它們的功能如下:(1)ChatClient.java:包含名為Chat(yī)Client的public類,其主要功能為定義客戶端的界面,添加大事偵聽與大事處理。該類定義了Connect()與DisConnect()方法實現(xiàn)與服務器的連接與斷開連接.當?shù)卿浀街付ǖ姆掌鲿r,調用ClientReceive類實現(xiàn)消息收發(fā),同時該類還定義了SendMessage()方法來向其他用戶發(fā)送帶有表情的消息或者靜靜話.(2)ClientReceive.java:該類是實現(xiàn)服務器端與客戶端消息收發(fā)的類。(3)ConnectConf.java:該類繼承自JDialog,是用戶對所要連接的服務器IP及偵聽端口進行修改配置的類。(4)UserConf.java:該類繼承自JDialog,是用戶對連接到服務器所顯示的用戶名進行修改配置的類。(5)Help。java:客戶端程序的幫助類。六.主要程序代碼清單和注釋說明:******************ChatServer.java****************************************?publicvoidactionPerformed(ActionEvente){??Objectobj=e。getSource();??if(obj==startServer||obj==startItem){//啟動服務端 ??startService();??}? elseif(obj==stopServer||obj==stopItem){//停止服務端 ? intj=JOptionPane.showConfirmDialog(?? ?this,"真的停止服務嗎?",”停止服務”,? ??JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);???if(j==JOptionPane。YES_OPTION){? ??stopService(); ??}??}? elseif(obj==portSet||obj==portItem){//端口設置???//調出端口設置的對話框 ? PortConfportConf=newPortConf(this);???portConf.show();??}??elseif(obj==exitButton||obj==exitItem){//退出程序???intj=JOptionPane.showConfirmDialog(????this,"真的要退出嗎?","退出”,? ?JOptionPane。YES_OPTION,JOptionPane。QUESTION_MESSAGE);?? if(j==JOptionPane.YES_OPTION){?? ?stopService();?? ?System。exit(0);? ?}??}? elseif(obj==helpItem){//菜單欄中的幫助? ?//調出幫助對話框? HelphelpDialog=newHelp(this);?? helpDialog。show();??}??elseif(obj==sysMessage||obj==sysMessageButton){//發(fā)送系統(tǒng)消息???sendSystemMessage(); ?} }?/**?*啟動服務端 */ publicvoidstartService(){??try{? ?serverSocket=newServerSocket(port,10);???messageShow。append(”服務端已經啟動,在"+port+"端口偵聽...\n");?? startServer.setEnabled(false);???startItem。setEnabled(false);???portSet。setEnabled(false);???portItem。setEnabled(false);???stopServer。setEnabled(true);???stopItem.setEnabled(true);?? sysMessage。setEnabled(true);??}??catch(Exceptione){???//System.out。println(e);??}? userLinkList=newUserLinkList();? listenThread=newServerListen(serverSocket,combobox,?? messageShow,showStatus,userLinkList);??listenThread.start();?}?/** *關閉服務端?*/ publicvoidstopService(){??try{???//向全部人發(fā)送服務器關閉的消息?? sendStopToAll();???listenThread.isStop=true;???serverSocket.close();? ?intcount=userLinkList。getCount();? ?inti=0; ??while(i〈count){????Nodenode=userLinkList.findUser(i);??? node.input.close();?? ?node。output.close(); ???node。socket.close(); ???i++;???}???stopServer.setEnabled(false); ??stopItem.setEnabled(false);???startServer.setEnabled(true); ??startItem。setEnabled(true);???portSet.setEnabled(true);???portItem.setEnabled(true);? ?sysMessage.setEnabled(false);???messageShow.append("服務端已經關閉\n");???combobox。removeAllItems(); ? combobox。addItem("全部人"); ?}? catch(Exceptione){ ??//System。out.println(e); ?} } /** *向全部人發(fā)送服務器關閉的消息?*/?publicvoidsendStopToAll(){ ?intcount=userLinkList。getCount();??inti=0;??while(i<count){ ? Nodenode=userLinkList。findUser(i);? ?if(node==null){??? i++;? ??continue;???}? ?try{??? node.output。writeObject("服務關閉");? ??node.output.flush();?? }???cat(yī)ch(Exceptione){????//System.out.println("$$$"+e);? ?}???i++;? } }?/**?*向全部人發(fā)送消息?*/?publicvoidsendMsgToAll(Stringmsg){? intcount=userLinkList.getCount();//用戶總數(shù) ?inti=0;??while(i〈count){???Nodenode=userLinkList.findUser(i);???if(node==null){????i++;?? continue;???}? ?try{? ?node.output.writeObject("系統(tǒng)信息");? ??node。output。flush();????node。output.writeObject(msg);????node。output.flush();? ?}???catch(Exceptione){? ??//System.out.println("@@@”+e);???} ? i++;??} ?sysMessage。setText("”);?} /**?*向客戶端用戶發(fā)送消息?*/ publicvoidsendSystemMessage(){ ?StringtoSomebody=combobox.getSelectedItem().toString();??Stringmessage=sysMessage。getText()+"\n";??messageShow.append(message);??//向全部人發(fā)送消息? if(toSomebody.equalsIgnoreCase("全部人”)){???sendMsgToAll(message);??}? else{???//向某個用戶發(fā)送消息?? Nodenode=userLinkList.findUser(toSomebody);???try{????node。output.writeObject("系統(tǒng)信息");? ??node.output.flush();????node.output.writeObject(message);? ??node.output。flush();???} ??catch(Exceptione){? ?//System.out.println(”!!!"+e);???}?? sysMessage。setText("”);//將發(fā)送消息欄的消息清空? }?}?publicstat(yī)icvoidmain(String[]args){? Chat(yī)Serverapp=newChatServer();?}}************************ServerListen.java:***************************publicclassServerListenextendsThread{?ServerSocketserver;??JComboBoxcombobox;?JTextAreatextarea;?JTextFieldtextfield;?UserLinkListuserLinkList;//用戶鏈表?Nodeclient; ServerReceiverecvThread;??publicbooleanisStop;?/*?*談天服務端的用戶上線于下線偵聽類?*/?publicServerListen(ServerSocketserver,JComboBoxcombobox,??JTextAreat(yī)extarea,JTextFieldtextfield,UserLinkListuserLinkList){??this.server=server; bobox=combobox; ?this.textarea=textarea;??this。textfield=textfield;??this.userLinkList=userLinkList; ??isStop=false;?}?publicvoidrun(){??while(!isStop&&!server。isClosed()){ ??try{????client=newNode();????client.socket=server.accept(); ? client。output=newObjectOutputStream(client.socket。getOutputStream()); ???client。output。flush();????client。input=newObjectInputStream(client.socket。getInputStream());????client.username=(String)client。input.readObject();????? ??//顯示提示信息????combobox.addItem(client.username);? ??userLinkList.addUser(client);????textarea。append("用戶"+client。username+"上線"+”\n"); ???textfield.setText(”在線用戶”+userLinkList.getCount()+"人\n”);??? ?? recvThread=newServerReceive(textarea,textfield,? ???combobox,client,userLinkList);?? ?recvThread。start(); ??} ? catch(Exceptione){?? }??} }}********************************ServerReceive.java***********************/**服務器收發(fā)消息的類*/publicclassServerReceiveextendsThread{ JTextAreatextarea;?JTextFieldtextfield;?JComboBoxcombobox; Nodeclient;?UserLinkListuserLinkList;//用戶鏈表?publicbooleanisStop;??publicServerReceive(JTextAreatextarea,JTextFieldtextfield, ?JComboBobobox,Nodeclient,UserLinkListuserLinkList){??this.textarea=textarea;? this。textfield=textfield; ?this。client=client;??this.userLinkList=userLinkList; bobox=combobox;? ??isStop=false;?}?publicvoidrun(){??//向全部人發(fā)送用戶的列表? sendUserList();??? while(!isStop&&!client.socket。isClosed()){? ?try{?? ?Stringtype=(String)client。input.readObject(); ???if(type.equalsIgnoreCase("談天信息")){?? ??StringtoSomebody=(String)client.input。readObject();??? Stringstat(yī)us=(String)client.input.readObject();?????Stringaction=(String)client.input。readObject();?????Stringmessage=(String)client。input.readObject();?? ?? ? ?Stringmsg=client.username??? ? ?+""+action????? +"對"???????+toSomebody ??? ?+"說:" ?? ? +message ?? ?? +”\n"; ? ?if(status。equalsIgnoreCase(”靜靜話")){?? ?? msg="[靜靜話]”+msg;?????}?????textarea.append(msg); ??? ??if(toSomebody.equalsIgnoreCase("全部人")){??????sendToAll(msg);//向全部人發(fā)送消息?????}???? else{??????try{ ??????client.output.writeObject(”談天信息”);?? ??? client.output.flush();??? ??client.output.writeObject(msg); ? client.output。flush();? ???} ?? ? catch(Exceptione){ ???? ?//System.out.println("###"+e);??? ??}? ???????? Nodenode=userLinkList。findUser(toSomebody);??? ? ??? ??if(node?。絥ull){???????node.output。writeObject("談天信息”); ? ?? node.output.flush();? ?????node。output.writeObject(msg);?? ? ??node.output.flush(); ?????} ????} ? ?}????elseif(type.equalsIgnoreCase("用戶下線")){?????Nodenode=userLinkList.findUser(client.username); ? ??userLinkList.delUser(node);??????????Stringmsg="用戶"+client.username+”下線\n"; ?intcount=userLinkList。getCount();?????combobox.removeAllItems();?????combobox.addItem("全部人");?????inti=0;?????while(i〈count){ ???? node=userLinkList.findUser(i);?? ???if(node==null){?? ??i++;? ??? ?continue;????? }??????combobox.addItem(node.username);?? ???i++;? ???}??? ?combobox.setSelectedIndex(0);?????textarea。append(msg); ???textfield。setText(”在線用戶"+userLinkList。getCount()+"人\n”);?????sendToAll(msg);//向全部人發(fā)送消息?????sendUserList();//重新發(fā)送用戶列表,刷新???? ? ???break;?? }???}?? cat(yī)ch(Exceptione){? ??//System。out。println(e);?? }? }?} /* *向全部人發(fā)送消息?*/?publicvoidsendToAll(Stringmsg){??intcount=userLinkList。getCount();????inti=0;??while(i<count){ ??Nodenode=userLinkList。findUser(i);???if(node==null){ ???i++;?? ?continue;???}???try{ ?? node。output.writeObject("談天信息");????node.output。flush();?? ?node。output。writeObject(msg);????node。output.flush();?? }? ?catch(Exceptione){??? //System。out.println(e);???} ? ? ?i++;? } }?/*?*向全部人發(fā)送用戶的列表?*/?publicvoidsendUserList(){??Stringuserlist="”; ?intcount=userLinkList。getCount(); ?inti=0; ?while(i<count){ ? Nodenode=userLinkList。findUser(i);?? if(node==null){????i++; ? continue;? ?}???userlist+=node。username;?? userlist+='\n'; ??i++;? }??i=0;??while(i〈count){ Nodenode=userLinkList.findUser(i);?? if(node==null){????i++; ??continue; ??}???try{??? node.output.writeObject(”用戶列表”); ? ?node.output.flush();?? ?node。output.writeObject(userlist);????node.output。flush();???}? ?catch(Exceptione){????//System.out.println(e);? }? ?i++; }?}}*************************PortConf.java***********************************/***生成端口設置對話框的類*/publicclassPortConfextendsJDialog{?JPanelpanelPort=newJPanel();?JButtonsave=newJButton();?JButtoncancel=newJButton(); publicstaticJLabelDLGINFO=newJLabel(??”默認端口號為:8888"); JPanelpanelSave=newJPanel();?JLabelmessage=newJLabel();?publicstaticJTextFieldportNumber;?publicPortConf(JFrameframe){ ?super(frame,true);??try{? ?jbInit();? }??catch(Exceptione){ ??e.printStackTrace(); ?} //設置運行位置,使對話框居中 DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();??this.setLocation((int)(screenSize.width—400)/2+50,???? ?(int)(screenSize.height-600)/2+150); this。setResizable(false);?}?privat(yī)evoidjbInit()throwsException{??this。setSize(newDimension(300,120));??this.setTitle("端口設置");? message。setText("請輸入偵聽的端口號:");??portNumber=newJTextField(10);? portNumber.setText(””+ChatServer.port);??save.setText("保存");??cancel.setText("取消");??panelPort。setLayout(newFlowLayout());??panelPort。add(message);? panelPort.add(portNumber);? panelSave。add(newLabel(””));??panelSave。add(save); panelSave.add(cancel);??panelSave.add(newLabel(""));??ContainercontentPane=getContentPane();??contentPane.setLayout(newBorderLayout()); ?contentPane.add(panelPort,BorderLayout。NORTH);? contentPane.add(DLGINFO,BorderLayout.CENTER); contentPane.add(panelSave,BorderLayout.SOUTH); ?//保存按鈕的大事處理? save.addActionListener( ??newActionListener(){????publicvoidactionPerformed(ActionEventa){? ? ?intsavePort;??? ?try{? ??? ?? ? ?savePort=Integer.parseInt(PortConf.portNumber.getText());? ????if(savePort<1||savePort〉65535){???????PortConf.DLGINFO.setText("偵聽端口必須是0-65535之間的整數(shù)?。?;???????PortConf.portNumber。setText("”);???? ?return;??? ??}??? ? ChatServer.port=savePort; ?? dispose(); ????}??? ?catch(NumberFormatExceptione){ ? ???PortConf。DLGINFO.setText("錯誤的端口號,端口號請?zhí)顚懻麛?shù)!");??????PortConf.portNumber.setText("");? ???return;?? ??}? }???} );??//關閉對話框時的操作 ?this.addWindowListener(???newWindowAdapter(){? ??publicvoidwindowClosing(WindowEvente){?? ??DLGINFO。setText(”默認端口號為:8888"); ??}? }??);? //取消按鈕的大事處理??cancel。addActionListener(???newActionListener(){? ??publicvoidactionPerformed(ActionEvente){??? ?DLGINFO。setText("默認端口號為:8888");?????dispose();??? }???});?}}**********************Node.java*****************************************/***用戶鏈表的結點類*/publicclassNode{?Stringusername=null;?Socketsocket=null;?ObjectOutputStreamoutput=null;?ObjectInputStreaminput=null; Nodenext=null;}**************************UserLinkList.java*****************************/***用戶鏈表*/publicclassUserLinkList{?Noderoot;?Nodepointer;?intcount;?/** *構造用戶鏈表?*/?publicUserLinkList(){ ?root=newNode();??root.next=null;??pointer=null;??count=0; }?/**?*添加用戶?*/?publicvoidaddUser(Noden){ ?pointer=root;?? ?while(pointer.next!=null){ ??pointer=pointer.next;??}??pointer.next=n;? n.next=null; ?count++;? ?} /**?*刪除用戶 */ publicvoiddelUser(Noden){? pointer=root;? while(pointer。next!=null){?? if(pointer。next==n){ ???pointer.next=n.next;??? count——;????????break;???}? pointer=pointer。next;??}?}?/**?*返回用戶數(shù)?*/?publicintgetCount(){??returncount;?}?/**?*依據(jù)用戶名查找用戶?*/?publicNodefindUser(Stringusername){? if(count==0)returnnull;? pointer=root; while(pointer。next!=null){ ? pointer=pointer.next;???if(pointer.username.equalsIgnoreCase(username)){? ??returnpointer; ??} ?}??returnnull; }?/**?*依據(jù)索引查找用戶?*/?publicNodefindUser(intindex){??if(count==0){? ?returnnull;? }? if(index〈0){ ?returnnull; ?}??pointer=root;??inti=0; ?while(i<index+1){?? if(pointer。next!=null){? ??pointer=pointer.next;? } ? else{ ??returnnull; }???i++; ?}? returnpointer;?}}**************************Help。java*************************************/***生成設置對話框的類*/publicclassHelpextendsJDialog{ JPaneltitlePanel=newJPanel();?JPanelcontentPanel=newJPanel(); JPanelclosePanel=newJPanel();?JButtonclose=newJButton(); JLabeltitle=newJLabel(”談天室服務端幫助");?JTextAreahelp=newJTextArea();?Colorbg=newColor(255,255,255);?publicHelp(JFrameframe){??super(frame,true);??try{ ??jbInit();??}??catch(Exceptione){ ??e.printStackTrace(); }? //設置運行位置,使對話框居中??DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();? this.setLocation((int)(screenSize.width-400)/2,? ?? ?(int)(screenSize.height-320)/2);? this。setResizable(false);?}?privatevoidjbInit()throwsException{? this。setSize(newDimension(400,200)); ?this.setTitle("幫助"); titlePanel.setBackground(bg);;??contentPanel.setBackground(bg);??closePanel.setBackground(bg); ?help.setText(”1、設置服務端的偵聽端口(默認端口為8888)。\n"+ ?”2、點擊啟動服務按鈕便可在指定的端口啟動服務.\n"+? ?”3、選擇需要接受消息的用戶,在消息欄中寫入消息,之后便可發(fā)送消息。\n"+???"4、信息狀態(tài)欄中顯示服務器當前的啟動與停止狀態(tài)、"+ ??”用戶發(fā)送的消息和\n服務器端發(fā)送的系統(tǒng)消息。");? help.setEditable(false);??titlePanel.add(newLabel(""));??titlePanel.add(title);??titlePanel.add(newLabel("”)); ?contentPanel.add(help);? closePanel.add(newLabel(""));??closePanel。add(close);??closePanel。add(newLabel("”));??ContainercontentPane=getContentPane();? contentPane.setLayout(newBorderLayout());? contentPane。add(titlePanel,BorderLayout.NORTH);??contentPane.add(contentPanel,BorderLayout.CENTER);? contentPane.add(closePanel,BorderLayout.SOUTH);??close.setText("關閉");? //大事處理??close.addActionListener(???newActionListener(){????publicvoidactionPerformed(ActionEvente){?? ??dispose();?? ?}???}); }}談天室客戶端設計:****************************Chat(yī)Client.java******************************/**談天客戶端的主框架類*/publicclassChat(yī)ClientextendsJFrameimplementsActionListener{?/**?*?*/?privatestaticfinallongserialVersionUID=1L;?Stringip="127.0。0.1”;//連接到服務端的ip地址 intport=8888;//連接到服務端的端口號?StringuserName="狂狂";//用戶名?inttype=0;//0表示未連接,1表示已連接?JComboBoxcombobox;//選擇發(fā)送消息的接受者?JTextAreamessageShow;//客戶端的信息顯示?JScrollPanemessageScrollPane;//信息顯示的滾動條?JLabelexpress,sendToLabel,messageLabel;?JTextFieldclientMessage;//客戶端消息的發(fā)送?JCheckBoxcheckbox;//靜靜話?JComboBoxactionlist;//表情選擇 JButtonclientMessageButton;//發(fā)送消息?JTextFieldshowStatus;//顯示用戶連接狀態(tài)?Socketsocket; ObjectOutputStreamoutput;//網(wǎng)絡套接字輸出流?ObjectInputStreaminput;//網(wǎng)絡套接字輸入流 ClientReceiverecvThread;?//建立菜單欄 JMenuBarjMenuBar=newJMenuBar(); //建立菜單組 JMenuoperat(yī)eMenu=newJMenu("操作(O)");?//建立菜單項 JMenuItemloginItem=newJMenuItem("用戶登錄(I)",newImageIcon("face/98.gif”)); JMenuItemlogoffItem=newJMenuItem("用戶注銷(L)",newImageIcon("face/icon.gif”));?JMenuItemexitItem=newJMenuItem("退出(X)”,newImageIcon("face/smile.gif"));?JMenuconMenu=newJMenu("設置(C)");?JMenuItemuserItem=newJMenuItem(”用戶設置(U)”,newImageIcon("face/messenger_big.gif”));?JMenuItemconnectItem=newJMenuItem(”連接設置(C)",newImageIcon(”face/Uabrand。gif”));?JMenuhelpMenu=newJMenu(”幫助(H)”); JMenuItemhelpItem=newJMenuItem(”幫助(H)”,newImageIcon("face/HelpCenter.gif"));?//建立工具欄 JToolBartoolBar=newJToolBar();?//建立工具欄中的按鈕組件?JButtonloginButton;//用戶登錄?JButtonlogoffButton;//用戶注銷 JButtonuserButton;//用戶信息的設置 JButtonconnectButton;//連接設置?JButtonexitButton;//退出按鈕?//框架的大小?DimensionfaceSize=newDimension(400,600);?JPaneldownPanel;?GridBagLayoutgirdBag; GridBagConstraintsgirdBagCon;?publicChatClient(){??init();//初始化程序??//添加框架的關閉大事處理? this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??this。pack(); ?//設置框架的大小??this.setSize(faceSize);??this.setVisible(true); ?setIconImage(getToolkit().getImage(”face/love.gif"));??//設置運行時窗口的位置? DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize(); ?this。setLocation((int)(screenSize。width-faceSize.getWidth())/2,? ? ?(int)(screenSize.height—faceSize。getHeight())/2); ?this.setResizable(false);??this.setTitle("談天室客戶端");//設置標題??//為操作菜單欄設置熱鍵’V'??operateMenu.setMnemonic(’O’);??//為用戶登錄設置快捷鍵為ctrl+i??loginItem.setMnemonic(’I’); ?loginItem.setAccelerator(KeyStroke。getKeyStroke(KeyEvent。VK_I,InputEvent.CTRL_MASK));? //為用戶注銷快捷鍵為ctrl+l logoffItem.setMnemonic(’L');? logoffItem.setAccelerat(yī)or(KeyStroke.getKeyStroke(KeyEvent.VK_L,InputEvent.CTRL_MASK));??//為退出快捷鍵為ctrl+x exitItem.setMnemonic(’X’);??exitItem.setAccelerat(yī)or(KeyStroke.getKeyStroke(KeyEvent。VK_X,InputEvent.CTRL_MASK));??//為設置菜單欄設置熱鍵’C'? conMenu.setMnemonic('C');? //為用戶設置設置快捷鍵為ctrl+u? userItem。setMnemonic(’U');??userItem。setAccelerator(KeyStroke.getKeyStroke(KeyEvent。VK_U,InputEvent.CTRL_MASK)); ?//為連接設置設置快捷鍵為ctrl+c??connectItem。setMnemonic('C');??connectItem.setAccelerat(yī)or(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); ?//為幫助菜單欄設置熱鍵'H’??helpMenu.setMnemonic(’H’);??//為幫助設置快捷鍵為ctrl+p??helpItem.setMnemonic(’H');??helpItem。setAccelerator(KeyStroke。getKeyStroke(KeyEvent.VK_H,InputEvent。CTRL_MASK));?} /**?*程序初始化函數(shù)?*/?publicvoidinit(){? ContainercontentPane=getContentPane();? contentPane.setLayout(newBorderLayout()); ?//添加菜單欄??operateMenu.add(loginItem); ?operateMenu。add(logoffItem);??operateMenu.addSeparator();??operateMenu.add(exitItem);? jMenuBar。add(operateMenu);??conMenu.add(userItem);??conMenu.addSeparator();??conMenu.add(connectItem);??jMenuBar.add(conMenu);? helpMenu.add(helpItem); ?jMenuBar。add(helpMenu);? setJMenuBar(jMenuBar);??//初始化按鈕??loginButton=newJButton("登錄”);??logoffButton=newJButton("注銷");??userButton=newJButton("用戶設置"); connectButton=newJButton("連接設置”);? exitButton=newJButton(”退出");??//當鼠標放上顯示信息? loginButton.setToolTipText(”連接到指定的服務器"); ?logoffButton。setToolTipText("與服務器斷開連接"); userButton.setToolTipText(”設置用戶信息”);? connectButton。setToolTipText("設置所要連接到的服務器信息"); ?//將按鈕添加到工具欄? toolBar.add(userButton); ?toolBar。add(connectButton);??toolBar.addSeparator();//添加分隔欄 toolBar。add(loginButton);??toolBar.add(logoffButton); ?toolBar.addSeparator();//添加分隔欄??toolBar。add(exitButton);??contentPane.add(toolBar,BorderLayout。NORTH); ?checkbox=newJCheckBox("靜靜話");??checkbox。setSelected(false); actionlist=newJComboBox(); ?actionlist.addItem(”微笑地");? actionlist.addItem(”高興地”);??actionlist.addItem(”輕輕地");??actionlist。addItem(”生氣地"); ?actionlist。setSelectedIndex(0); ?//初始時??loginButton.setEnabled(true);??logoffButton。setEnabled(false);? //為菜單欄添加大事監(jiān)聽 ?loginItem.addActionListener(this);??logoffItem.addActionListener(this);??exitItem。addActionListener(this);??userItem。addActionListener(this);??connectItem.addActionListener(this);??helpItem.addActionListener(this);??//添加按鈕的大事偵聽??loginButton.addActionListener(this);??logoffButton.addActionListener(this); userButton。addActionListener(this); ?connectButton.addActionListener(this);??exitButton.addActionListener(this);??combobox=newJComboBox();? combobox.insertItemAt("全部人",0);??combobox。setSelectedIndex(0); ?messageShow=newJTextArea();? messageShow.setEditable(false);? //添加滾動條??messageScrollPane=newJScrollPane(messageShow, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, ??JScrollPane。HORIZONTAL_SCROLLBAR_AS_NEEDED);??messageScrollPane.setPreferredSize(newDimension(400,400)); messageScrollPane.revalidate();??clientMessage=newJTextField(23);??clientMessage.setEnabled(false); ?clientMessageButton=newJButton();? clientMessageButton.setText("發(fā)送”);??//添加系統(tǒng)消息的大事偵聽 ?clientMessage.addActionListener(this);??clientMessageButton.addActionListener(this);??sendToLabel=newJLabel("發(fā)送至:");? express=newJLabel("表情:");??messageLabel=newJLabel("發(fā)送消息:”); ?downPanel=newJPanel(); ?girdBag=newGridBagLayout(); ?downPanel。setLayout(girdBag);??girdBagCon=newGridBagConstraints(); ?girdBagCon.gridx=0; ?girdBagCon.gridy=0;? girdBagCon.gridwidth=5;??girdBagCon.gridheight=2;??girdBagCon.ipadx=5;??girdBagCon。ipady=5;??JLabelnone=newJLabel("”);??girdBag.setConstraints(none,girdBagCon);??downPanel.add(none);??girdBagCon=newGridBagConstraints(); ?girdBagCon.gridx=0;? girdBagCon。gridy=2;??girdBagCon.insets=newInsets(1,0,0,0);? //girdBagCon。ipadx=5;??//girdBagCon。ipady=5; ?girdBag。setConstraints(sendToLabel,girdBagCon);??downPanel。add(sendToLabel);? girdBagCon=newGridBagConstraints();? girdBagCon.gridx=1;??girdBagCon.gridy=2;??girdBagCon。anchor=GridBagConstraints.LINE_START;? girdBag.setConstraints(combobox,girdBagCon); downPanel.add(combobox);??girdBagCon=newGridBagConstraints();??girdBagCon.gridx=2; girdBagCon.gridy=2;? girdBagCon.anchor=GridBagConstraints.LINE_END;??girdBag.setConstraints(express,girdBagCon);??downPanel.add(express);??girdBagCon=newGridBagConstraints(); ?girdBagCon.gridx=3;??girdBagCon。gridy=2;??girdBagCon.anchor=GridBagConstraints。LINE_START;? //girdBagCon.insets=newInsets(1,0,0,0);? //girdBagCon.ipadx=5;??//girdBagCon.ipady=5;??girdBag.setConstraints(actionlist,girdBagCon);??downPanel.add(actionlist);? girdBagCon=newGridBagConstraints();??girdBagCon。gridx=4;? girdBagCon。gridy=2;??girdBagCon.insets=newInsets(1,0,0,0);??//girdBagCon。ipadx=5;? //girdBagCon.ipady=5; ?girdBag。setConstraints(checkbox,girdBagCon);??downPanel.add(checkbox); ?girdBagCon=newGridBagConstraints(); ?girdBagCon.gridx=0;??girdBagCon.gridy=3;? girdBag.setConstraints(messageLabel,girdBagCon); ?downPanel。add(messageLabel);??girdBagCon=newGridBagConstraints(); ?girdBagCon.gridx=1;? girdBagCon。gridy=3;??girdBagCon.gridwidth=3;??girdBagCon.gridheight=1;??girdBag.setConstraints(clientMessage,girdBagCon);??downPanel.add(clientMessage); ?gi

溫馨提示

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

評論

0/150

提交評論