《面向?qū)ο蟪绦蛟O(shè)計課程設(shè)計》_第1頁
《面向?qū)ο蟪绦蛟O(shè)計課程設(shè)計》_第2頁
《面向?qū)ο蟪绦蛟O(shè)計課程設(shè)計》_第3頁
《面向?qū)ο蟪绦蛟O(shè)計課程設(shè)計》_第4頁
《面向?qū)ο蟪绦蛟O(shè)計課程設(shè)計》_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quá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(藍色”);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+進入+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(藍色”);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)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論