




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 .系統(tǒng)實現(xiàn)。(附上具體源代碼)3.1.MDS_ServerChat.javapackagemadingsheng;importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;.*;importjava.io.*;publicclassMDSServerChatextendsJFrameimplementsActionListener,Runnableprivateintport=8888;privateServerSocketserver;privateSocketclient;privateBufferedReadercin;pr
2、ivatePrintWritercout;privateThreadchatThread;privateJPanelp1;privateJButtonstartButton;privateJButtonstopButton;privateJButtonexitButton;privateJScrollPanescrollPanel;privateJTextAreamessage;privateJPanelp2;privateJTextFieldserverMessage;privateJButtonmessageButton;privateJPanelp3;privateJComboBoxco
3、mbox1;privateJComboBoxcombox2;privateJMenuBarmenubar;privateJMenumenu,menu1;privateJMenuItemitem1,item2,item3,item4,item5;publicMDS_ServerChat()super(網(wǎng)絡(luò)聊天服務(wù)器);menubar=newJMenuBar();menu=newJMenu(更換背景色”);menu1=newJMenu(幫助);item1=newJMenuItem(灰色”);item2=newJMenuItem(藍(lán)色”);item3=newJMenuItem(白色”);item4=
4、newJMenuItem(時間);item5=newJMenuItem(其他);menu.add(item1);menu.add(item2);menu.add(item3);menu1.add(item4);menu1.add(item5);menubar.add(menu);menubar.add(menu1);setJMenuBar(menubar);item1.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)message.setBackground(Color.gray););it
5、em2.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)message.setBackground(Color.blue););item3.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)message.setBackground(Color.white););p1=newJPanel();startButton=newJButton(啟動);stopButton=newJButton(停
6、止);exitButton=newJButton(退出);startButton.addActionListener(this);stopButton.addActionListener(this);exitButton.addActionListener(this);p1.add(startButton);p1.add(stopButton);p1.add(exitButton);add(p1,BorderLayout.NORTH);message=newJTextArea();scrollPanel=newJScrollPane(message,JScrollPane.VERTICAL_S
7、CROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);add(scrollPanel,BorderLayout.CENTER);p2=newJPanel();serverMessage=newJTextField(15);messageButton=newJButton(發(fā)送消息”);messageButton.addActionListener(this);p2.add(serverMessage);p2.add(messageButton);add(p2,BorderLayout.SOUTH);p3=newJPanel
8、();combox1=newJComboBox();combox2=newJComboBox();combox1.addItem(-在線人數(shù)-);comboxl.addItem(不祥);comboxl.addItem(不祥);comboxl.addItem(不詳);p3.add(combox1);add(p3,BorderLayout.EAST);p3.setBackground(Color.gray);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(800,100,500,400);setVisible(true);publi
9、cvoidactionPerformed(ActionEvente)if(e.getSource()=startButton)p1.setBackground(Color.green);tryserver=newServerSocket(port);/client=server.accept();catch(Exceptionex)chatThread=newThread(this);chatThread.start();startButton.setVisible(false);message.append(+n);elseif(e.getSource()=stopButton)chatTh
10、read=null;startButton.setVisible(true);if(cout!=null)cout.println(stop);tryif(server!=null)p1.setBackground(Color.red);server.close();catch(Exceptionexp)elseif(e.getSource()=exitButton)chatThread=null;if(cout!=null)cout.println(exit);System.exit(0);elseif(e.getSource()=messageButton)if(chatThread!=n
11、ull)cout.println(serverMessage.getText()+n);message.append(:+serverMessage.getText()+n);publicvoidrun()while(true)tryclient=server.accept();ServerChatThreadsct=newServerChatThread(client);Threadt=newThread(sct);t.start();catch(Exceptionex)publicstaticvoidmain(Stringargs)MDS_ServerChatserverChat=newM
12、DS_ServerChat();classServerChatThreadimplementsRunnableprivateSocketclient2;ServerChatThread(Socketclient)client2=client;publicvoidrun()while(true)trycin=newBufferedReader(newInputStreamReader(client2.getInputStream();cout=newPrintWriter(client2.getOutputStream(),true);Stringusername=cin.readLine();
13、if(username!=null)message.append(客戶:+username+進(jìn)入+n);cout.println(ok);Stringaline=;while(chatThread!=null)aline=cin.readLine();if(aline.equals(exit)break;elsemessage.append(username+說:+aline);catch(Exceptione)3.2.MDS_ClientChat.javapackagemadingsheng;importjava.awt.*;importjava.awt.event.*;importjava
14、x.swing.*;.*;importjava.io.*;publicclassMDSClientChatextendsJFrameimplementsActionListener.Runnableprivateintport=8888;privateSocketclient;privateBufferedReadercin;privatePrintWritercout;privateThreadchatThread;privateJPanelp1;privateJLabelusernameLabel;privateJTextFieldusername;privateJButtonloginB
15、utton;privateJButtonexitButton;privateJScrollPanescrollPanel;privateJTextAreamessage;privateJPanelp2;privateJTextFieldclientMessage;privateJButtonclientButton;privateJMenuBarmenubar;privateJMenumenu,menu1;privateJMenuItemitem1,item2,item3,item4,item5;publicMDS_ClientChat()super(網(wǎng)絡(luò)聊天客戶端”);menubar=new
16、JMenuBar();menu=newJMenu(更換背景色”);menu1=newJMenu(幫助);item1=newJMenuItem(灰色”);item2=newJMenuItem(藍(lán)色”);item3=newJMenuItem(白色”);item4=newJMenuItem(時間);item5=newJMenuItem(其他);menu.add(item1);menu.add(item2);menu.add(item3);menu1.add(item4);menu1.add(item5);menubar.add(menu);menubar.add(menu1);setJMenuBar
17、(menubar);item1.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)message.setBackground(Color.gray););item2.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)message.setBackground(Color.blue););item3.addActionListener(newActionListener()publicvoida
18、ctionPerformed(ActionEvente)message.setBackground(Color.white););p1=newJPanel();usernameLabel=newJLabel(用戶名);username=newJTextField(10);loginButton=newJButton(登錄”);exitButton=newJButton(退出);loginButton.addActionListener(this);exitButton.addActionListener(this);p1.add(usernameLabel);p1.add(username);
19、p1.add(loginButton);p1.add(exitButton);add(p1,BorderLayout.NORTH);message=newJTextArea();scrollPanel=newJScrollPane(message,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);add(scrollPanel,BorderLayout.CENTER);p2=newJPanel();clientMessage=newJTextField(15);clientB
20、utton=newJButton(發(fā)送消息);clientButton.addActionListener(this);p2.add(clientMessage);p2.add(clientButton);add(p2,BorderLayout.SOUTH);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,500,400);setVisible(true);publicvoidactionPerformed(ActionEvente)if(e.getSource()=loginButton)if(loginSer
21、ver(username.getText()p1.setBackground(Color.green);chatThread=newThread(this);chatThread.start();elseif(e.getSource()=exitButton)chatThread=null;if(cout!=null)cout.println(exit);System.exit(0);elseif(e.getSource()=clientButton)if(chatThread!=null)cout.println(clientMessage.getText();message.append(
22、username.getText()+發(fā)送了+clientMessage.getText()+n);publicbooleanloginServer(Stringusername)tryclient=newSocket(localhost,port);cin=newBufferedReader(newInputStreamReader(client.getInputStream();cout=newPrintWriter(client.getOutputStream(),true);cout.println(username);Stringaline=cin.readLine();if(ali
23、ne.equals(ok)message.append(連接到服務(wù)器+n);returntrue;elsemessage.append(連接到服務(wù)器+n);returnfalse;catch(Exceptione)returnfalse;publicvoidrun()Stringaline=;trywhile(chatThread!=null&(aline=cin.readLine()!=null)if(aline.equals(stop)|aline.equals(exit)p1.setBackground(Color.red);message.append(服務(wù)器已中斷,請重新連接);break;elsemessage.append(message.append(服務(wù)器說:+aline);cin.close();cout.close();client.close();catch(Exceptione)publicstaticvoidmain(Stringargs)MDSClientChatcc=newMDSClientChat();4.課程總結(jié)。(對所完成的課程設(shè)計做較完整的總結(jié)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- (高清版)DB34∕T 5200-2025 家政技能競賽服務(wù)規(guī)范
- 兒童心腦血管疾病的早期發(fā)現(xiàn)與處理
- 江蘇省連云港市2025-2026學(xué)年七年級上學(xué)期分班摸底考試英語試卷(無答案)
- 崗位爭先活動方案
- 工程品質(zhì)提升年活動方案
- 小學(xué)生下鄉(xiāng)扶貧活動方案
- 展覽特色活動方案
- 小班家長會活動方案
- 歲末沖刺活動方案
- 工會洗滌活動方案
- 2024年河北省中考語文試題(含答案解析)
- 08SS523建筑小區(qū)塑料排水檢查井
- 腸內(nèi)營養(yǎng)堵管的護理方法
- 《體育與健康》水平四教學(xué)設(shè)計
- 胸腔積液護理查房
- 醫(yī)療護理員基礎(chǔ)理論知識考試試題題庫及答案
- 制造業(yè)暑假工合同
- 湖南省張家界市永定區(qū)2023-2024學(xué)年三年級下學(xué)期期末考試數(shù)學(xué)試題
- 2024年湖北省中考?xì)v史真題
- 瀘州老窖“濃香文釀杯”企業(yè)文化知識競賽考試題庫大全-上(單選題)
- 2024小學(xué)六年級人教版道德與法治升學(xué)畢業(yè)小升初試卷及答案(時政+上下冊考點)04
評論
0/150
提交評論