![java課程設(shè)計(jì)-聊天軟件(帶源碼)_第1頁(yè)](http://file4.renrendoc.com/view/2ce23cecbb7f8af4ff251a0b597bce5c/2ce23cecbb7f8af4ff251a0b597bce5c1.gif)
![java課程設(shè)計(jì)-聊天軟件(帶源碼)_第2頁(yè)](http://file4.renrendoc.com/view/2ce23cecbb7f8af4ff251a0b597bce5c/2ce23cecbb7f8af4ff251a0b597bce5c2.gif)
![java課程設(shè)計(jì)-聊天軟件(帶源碼)_第3頁(yè)](http://file4.renrendoc.com/view/2ce23cecbb7f8af4ff251a0b597bce5c/2ce23cecbb7f8af4ff251a0b597bce5c3.gif)
![java課程設(shè)計(jì)-聊天軟件(帶源碼)_第4頁(yè)](http://file4.renrendoc.com/view/2ce23cecbb7f8af4ff251a0b597bce5c/2ce23cecbb7f8af4ff251a0b597bce5c4.gif)
![java課程設(shè)計(jì)-聊天軟件(帶源碼)_第5頁(yè)](http://file4.renrendoc.com/view/2ce23cecbb7f8af4ff251a0b597bce5c/2ce23cecbb7f8af4ff251a0b597bce5c5.gif)
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
實(shí)驗(yàn)設(shè)計(jì)目的:(1)掌握類(lèi)的定義和使用;(2)掌握對(duì)象的定義;(3)掌握線(xiàn)程的使用。實(shí)驗(yàn)設(shè)計(jì)內(nèi)容:設(shè)計(jì)一個(gè)類(lèi)似qq群聊的聊天軟件技術(shù)要點(diǎn):客戶(hù)端和服務(wù)器端的開(kāi)發(fā),數(shù)據(jù)庫(kù)的鏈接與使用實(shí)驗(yàn)條件:(1)主要設(shè)備:586或更高機(jī)型,256MB或更高的內(nèi)存,40G或更大的硬盤(pán)。(2)主要軟件:①操作系統(tǒng)可為Windows9X、WinMe、Win2000或更高版本等;②開(kāi)發(fā)環(huán)境為jdk或者jcreator。(3)參考書(shū)目:①尹繼平,張帆.java范例大全.機(jī)械工業(yè)出版社②施霞萍.java程序設(shè)計(jì)教程.機(jī)械工業(yè)出版社實(shí)驗(yàn)方法與步驟:這個(gè)軟件從0.1到1.3一共13個(gè)版本。0.1到0.4版本為客戶(hù)端界面設(shè)計(jì),第0.5到1.2版本為服務(wù)器端設(shè)計(jì)以及客戶(hù)端與服務(wù)器端通信連接的設(shè)計(jì)實(shí)現(xiàn)。1.3版為完善之前版本的缺陷并添加登陸界面。最終版本1.3版中一共三個(gè)類(lèi)(如圖一所示)源代碼如下://客戶(hù)端importjava.awt.*;importjava.awt.event.*;importjava.io.IOException;import.*;importjava.io.*;publicclassChatClientextendsFrame{ Sockets=null; DataOutputStreamdos=null; DataInputStreamdis=null; privatebooleanbConnected=false; privatestaticfinallongserialVersionUID=1L; TextFieldtfTxt=newTextField(); TextAreataContent=newTextArea(); publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub newLogin(); } publicvoidlaunchFrame(){ setLocation(400,300); this.setSize(300,300); add(tfTxt,BorderLayout.SOUTH); add(taContent,BorderLayout.NORTH); pack(); this.addWindowListener(newWindowAdapter(){ @Override publicvoidwindowClosing(WindowEvente){ disconnect(); System.exit(0); } }); tfTxt.addActionListener(newTFListener()); setVisible(true); connect(); newThread(newRecvThead()).start(); } publicvoidconnect(){ try{ s=newSocket("",8888); dos=newDataOutputStream(s.getOutputStream()); dis=newDataInputStream(s.getInputStream()); System.out.print("lianjieshangle"); bConnected=true; }catch(UnknownHostExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } publicvoiddisconnect(){ try{ dos.close(); s.close(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } privateclassTFListenerimplementsActionListener{ publicvoidactionPerformed(ActionEvente){ Stringip=null; Stringaddress=null; InetAddressaddr; try{ addr=InetAddress.getLocalHost(); ip=addr.getHostAddress().toString();//獲得本機(jī)IP address=addr.getHostName().toString();//獲得本機(jī)名稱(chēng) }catch(UnknownHostExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); } Stringstr=address+ip+"\n"+tfTxt.getText().trim(); // taContent.setText(str); tfTxt.setText(""); try{ //DataOutputStreamdos=new //DataOutputStream(s.getOutputStream()); dos.writeUTF(str); dos.flush(); //dos.close(); }catch(IOExceptione1){ //TODOAuto-generatedcatchblock e1.printStackTrace(); } } } privateclassRecvTheadimplementsRunnable{ @Override publicvoidrun(){ try{ while(bConnected){ Stringstr; str=dis.readUTF(); taContent.setText(taContent.getText()+str+'\n'); System.out.print(str); } }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } }}//服務(wù)器端importjava.io.DataInputStream;importjava.io.EOFException;importjava.io.IOException;importjava.io.*;import.*;importjava.util.*;publicclassChatServer{ booleanstarted=false; ServerSocketss=null; List<Client>clients=newArrayList<Client>(); publicstaticvoidmain(String[]args){ //Sockets=null; //DataInputStreamdis=null; newChatServer().start(); } publicvoidstart(){ try{ ss=newServerSocket(8888); started=true; }catch(BindExceptione){ System.out.print("端口使用中!\n"); System.out.print("請(qǐng)關(guān)閉相關(guān)程序重新運(yùn)行程序!\n"); System.exit(0); }catch(IOExceptione){ e.printStackTrace(); } try{ while(started){ //ooleanbConnected=false; //s=ss.accept(); Sockets=ss.accept(); Clientc=newClient(s); System.out.print("已連接!\n"); newThread(c).start(); clients.add(c); } //dis.close(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ ss.close(); }catch(IOExceptione1){ e1.printStackTrace(); } } } classClientimplementsRunnable{ privateSockets; privateDataInputStreamdis=null; privateDataOutputStreamdos=null; privatebooleanbConnected=false; publicClient(Sockets){ this.s=s; try{ dis=newDataInputStream(s.getInputStream()); dos=newDataOutputStream(s.getOutputStream()); bConnected=true; }catch(IOExceptione){ e.printStackTrace(); } } publicvoidsend(Stringstr){ try{ dos.writeUTF(str); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } publicvoidrun(){ try{ while(bConnected){ Stringstr=dis.readUTF();System.out.print(str+"\n"); for(inti=0;i<clients.size();i++){ Clientc=clients.get(i); c.send(str); } } }catch(EOFExceptione){ System.out.print("客戶(hù)端斷開(kāi)連接!\n"); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ if(dis!=null) dis.close(); if(dos!=null) dos.close(); //if(s!=null)s.close(); }catch(IOExceptione1){ e1.printStackTrace(); } } } }}//客戶(hù)端登陸*Tochangethistemplate,chooseTools|Templatesimportjavax.swing.*;/****@authorAdministrator*/publicclassLoginextendsJFrameimplementsActionListener{JPanelpnlLogin;JLabellblUserName,lblPassword,P;JButtonbtnLogin,btnExit;JTextFieldtxtUserName,txtlbldl;JPasswordFieldpwdPassword;DimensiondsSize;Toolkittoolkit=Toolkit.getDefaultToolkit();publicLogin(){super("登陸");pnlLogin=newJPanel();this.getContentPane().add(pnlLogin);pnlLogin.setLayout(null);lblUserName=newJLabel("用戶(hù)名(U):");lblPassword=newJLabel("密碼:");txtUserName=newJTextField(20);pwdPassword=newJPasswordField(30);btnLogin=newJButton("登錄(L)");btnLogin.setMnemonic('L');btnExit=newJButton("退出(X)");btnExit.setToolTipText("退出系統(tǒng)");btnExit.setMnemonic('X');btnLogin.addActionListener(this);btnExit.addActionListener(this);//P.setBounds(0,0,315,120);//pnlLogin.add(P);pnlLogin.setBackground(Color.WHITE);lblUserName.setBounds(10,125,90,25);txtUserName.setBounds(120,125,180,25);lblPassword.setBounds(10,155,90,25);pwdPassword.setBounds(120,155,180,25);btnLogin.setBounds(20,185,80,25);btnExit.setBounds(220,185,80,25);lblUserName.setForeground(Color.BLACK);lblUserName.setBackground(Color.WHITE);lblPassword.setForeground(Color.BLACK);lblPassword.setBackground(Color.WHITE);pnlLogin.add(lblUserName);pnlLogin.add(txtUserName);pnlLogin.add(lblPassword);pnlLogin.add(pwdPassword);pnlLogin.add(btnLogin);pnlLogin.add(btnExit);setResizable(false);setSize(315,245);setVisible(true);dsSize=toolkit.getScreenSize();setLocation(dsSize.width/2-this.getWidth()/2,dsSize.height/2-this.getHeight()/2);}@OverridepublicvoidactionPerformed(ActionEventae){if(ae.getSource()==btnLogin){Stringjusername=txtUserName.getText().trim();char[]s=pwdPassword.getPassword();Stringjpassword=newString(s);if(jusername.equals("")||jpassword.equals("")){JOptionPane.showMessageDialog(this,"對(duì)不起,請(qǐng)輸入用戶(hù)名或密碼.","錯(cuò)誤!",JOptionPane.ERROR_MESSAGE);}else{try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//System.out.println("加載驅(qū)動(dòng)程序成功");}catch(Exceptione){System.out.println("無(wú)法加載驅(qū)動(dòng)程序");}try{ Stringurl="jdbc:sqlserver://localhost:1433;DatabaseName=login"; //pubs為你的數(shù)據(jù)庫(kù)的 Stringuser="";//用自己的數(shù)據(jù)庫(kù)登錄賬戶(hù)和密碼 Stringpassword="";//Connectionconn=DriverManager.getConnection(url,user,password);Statementstmt=conn.createStatement();//if(jusername.length()<30){// for(inti=0;i<27;i++)// jusername=jusername+"";//}ResultSetrs=stmt.executeQuery("selectusername,passwordfromlogin_userwhereusername='"+jusername+"'");if(rs.next()){ Stringa=rs.getString("password").trim();if(a.equals(jpassword)){JOptionPane.showMessageDialog(null,"登陸成功");newChatClient().launchFrame();super.setVisible(false);}else{JOptionPane.showMessageDialog(this,"對(duì)不起,密碼錯(cuò)誤,請(qǐng)重新輸入","登陸失敗",JOptionPane.ERROR_MESSAGE);}}else{JOptionPane.showMessageDialog
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 湘教版地理八年級(jí)下冊(cè)7.4《長(zhǎng)江三角洲區(qū)域的內(nèi)外聯(lián)系》(第2課時(shí))聽(tīng)課評(píng)課記錄
- 北師大版道德與法治七年級(jí)下冊(cè)9.1《我們身邊的法律》聽(tīng)課評(píng)課記錄
- 湘教版數(shù)學(xué)九年級(jí)下冊(cè)聽(tīng)評(píng)課記錄:2.3 垂徑定理
- 小學(xué)二年級(jí)上冊(cè)數(shù)學(xué)口算練習(xí)題人教版新課標(biāo)
- 小學(xué)二年級(jí)人教版口算及豎式計(jì)算寒假練習(xí)A4排版
- 小學(xué)二年級(jí)加減乘法口算練習(xí)題
- 蘇教版小學(xué)二年級(jí)數(shù)學(xué)上冊(cè)口算題卡
- 超市連鎖加盟合同范本
- 儲(chǔ)藏室租賃合同范本
- 汽車(chē)二級(jí)經(jīng)銷(xiāo)商合作協(xié)議書(shū)范本
- 高標(biāo)準(zhǔn)農(nóng)田施工組織設(shè)計(jì)(全)
- 宿舍、辦公樓消防應(yīng)急預(yù)案
- 細(xì)胞全能性的課件資料
- 職業(yè)安全健康工作總結(jié)(2篇)
- 14S501-1 球墨鑄鐵單層井蓋及踏步施工
- YB 4022-1991耐火泥漿荷重軟化溫度試驗(yàn)方法(示差-升溫法)
- 水土保持方案中沉沙池的布設(shè)技術(shù)
- 安全生產(chǎn)技術(shù)規(guī)范 第25部分:城鎮(zhèn)天然氣經(jīng)營(yíng)企業(yè)DB50-T 867.25-2021
- 現(xiàn)代企業(yè)管理 (全套完整課件)
- 走進(jìn)本土項(xiàng)目化設(shè)計(jì)-讀《PBL項(xiàng)目化學(xué)習(xí)設(shè)計(jì)》有感
- 高中語(yǔ)文日積月累23
評(píng)論
0/150
提交評(píng)論