《Java程序設(shè)計(jì)》課程設(shè)計(jì)大作業(yè)-服務(wù)端聊天系統(tǒng).doc_第1頁(yè)
《Java程序設(shè)計(jì)》課程設(shè)計(jì)大作業(yè)-服務(wù)端聊天系統(tǒng).doc_第2頁(yè)
《Java程序設(shè)計(jì)》課程設(shè)計(jì)大作業(yè)-服務(wù)端聊天系統(tǒng).doc_第3頁(yè)
《Java程序設(shè)計(jì)》課程設(shè)計(jì)大作業(yè)-服務(wù)端聊天系統(tǒng).doc_第4頁(yè)
《Java程序設(shè)計(jì)》課程設(shè)計(jì)大作業(yè)-服務(wù)端聊天系統(tǒng).doc_第5頁(yè)
已閱讀5頁(yè),還剩16頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

此文檔收集于網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系網(wǎng)站刪除20112012學(xué)年第一學(xué)期Java程序設(shè)計(jì)課程大作業(yè)題目: 服務(wù)端聊天系統(tǒng) 專業(yè): 計(jì)算機(jī) 班級(jí): 計(jì)算機(jī)1班 學(xué)號(hào): 090400224 姓名: 馬英俊 教師: 楊樹(shù)林 日期:2011年 12月 22 日項(xiàng)目?jī)?nèi)容單項(xiàng)分值總分選題1題目明確,系統(tǒng)功能較集中482題目難度適宜,有一定的綜合性4功能實(shí)現(xiàn)3功能完整,能夠正確運(yùn)行6244模塊劃分合理,程序結(jié)構(gòu)清晰65界面設(shè)計(jì)合理、易用、協(xié)調(diào)66設(shè)計(jì)與實(shí)現(xiàn)程序6規(guī)范性及文檔7代碼規(guī)范性及注釋5108文檔內(nèi)容齊全,格式規(guī)范5總體效果9完成總體質(zhì)量61210軟件功能及技術(shù)水平6答辯11講解清楚明了,功能演示流暢66評(píng)語(yǔ)總分一 課程設(shè)計(jì)目的JAVA程序設(shè)計(jì)是計(jì)算機(jī)相關(guān)專業(yè)的必修專業(yè)基礎(chǔ)課程,其實(shí)踐性、應(yīng)用性很強(qiáng)。實(shí)踐是必不可少的一個(gè)重要環(huán)節(jié)。JAVA程序設(shè)計(jì)的設(shè)計(jì)目的是加深對(duì)理論內(nèi)容的理解和掌握,更加系統(tǒng)地掌握程序設(shè)計(jì)及其在開(kāi)發(fā)中的廣泛應(yīng)用,基本方法及技巧,為綜合運(yùn)用所學(xué)知識(shí),利用軟件工程為基礎(chǔ)進(jìn)行軟件開(kāi)發(fā)、并在實(shí)踐應(yīng)用方面打下一定基礎(chǔ)。二 課程設(shè)計(jì)的實(shí)驗(yàn)環(huán)境運(yùn)行windows 2007或XP以上微機(jī)系統(tǒng),JDK,NETBEAN。三 課程設(shè)計(jì)主要內(nèi)容Java語(yǔ)言的學(xué)習(xí)熱潮并沒(méi)有因?yàn)闀r(shí)間的推移而消退,相反,由于計(jì)算機(jī)技術(shù)的發(fā)展所帶來(lái)新應(yīng)用的出現(xiàn),Java越來(lái)越流行,這種情況是以往程序設(shè)計(jì)語(yǔ)言在其生存周期內(nèi)所不多見(jiàn)的。Java語(yǔ)言之所以這樣長(zhǎng)盛不衰,一是因?yàn)槠浔姸嗟募夹g(shù)特點(diǎn)與現(xiàn)今的應(yīng)用十分合拍,可以覆蓋大部分的需求;二是因?yàn)镾UN公司不斷推出新的版本,完善Java自身的功能。有了這兩點(diǎn),Java語(yǔ)言成為程序員首選的程序設(shè)計(jì)開(kāi)發(fā)工具就在情理之中了。服務(wù)端聊天系統(tǒng)是一個(gè)方便于兩端用戶溝通的軟件,通過(guò)輸入偵聽(tīng)方的端口號(hào)(例如:8888)來(lái)啟動(dòng)服務(wù)。當(dāng)服務(wù)啟動(dòng)時(shí),可進(jìn)行聊天;當(dāng)停止服務(wù)時(shí),聊天信息將不再接受。四 詳細(xì)設(shè)計(jì)程序詳細(xì)設(shè)計(jì)代碼如下:package org.Adam;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import .*;import java.io.*;import org.Adam.PortConf;import org.Adam.ServerListen;/* * * author Administrator */public class ChatServer extends JFrame implements ActionListenerpublic static int port = 8888;/服務(wù)端的偵聽(tīng)端口ServerSocket serverSocket;/服務(wù)端SocketImage icon;/程序圖標(biāo)JComboBox combobox;/選擇發(fā)送消息的接受者JTextArea messageShow;/服務(wù)端的信息顯示JScrollPane messageScrollPane;/信息顯示的滾動(dòng)條JTextField showStatus;/顯示用戶連接狀態(tài)JLabel sendToLabel,messageLabel;JTextField sysMessage;/服務(wù)端消息的發(fā)送JButton sysMessageButton;/服務(wù)端消息的發(fā)送按鈕UserLinkList userLinkList;/用戶鏈表/建立菜單欄JMenuBar jMenuBar = new JMenuBar(); /建立菜單組JMenu serviceMenu = new JMenu (服務(wù)(V); /建立菜單項(xiàng)JMenuItem portItem = new JMenuItem (端口設(shè)置(P);JMenuItem startItem = new JMenuItem (啟動(dòng)服務(wù)(S);JMenuItem stopItem=new JMenuItem (停止服務(wù)(T);JMenuItem exitItem=new JMenuItem (退出(X);JMenu helpMenu=new JMenu (幫助(H);JMenuItem helpItem=new JMenuItem (幫助(H);/建立工具欄JToolBar toolBar = new JToolBar();/建立工具欄中的按鈕組件JButton portSet;/啟動(dòng)服務(wù)端偵聽(tīng)JButton startServer;/啟動(dòng)服務(wù)端偵聽(tīng)JButton stopServer;/關(guān)閉服務(wù)端偵聽(tīng)JButton exitButton;/退出按鈕/框架的大小Dimension faceSize = new Dimension(400, 600);ServerListen listenThread;JPanel downPanel ;GridBagLayout girdBag;GridBagConstraints girdBagCon;/* * 服務(wù)端構(gòu)造函數(shù) */public ChatServer()init();/初始化程序/添加框架的關(guān)閉事件處理this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.pack();/設(shè)置框架的大小this.setSize(faceSize);/設(shè)置運(yùn)行時(shí)窗口的位置Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();this.setLocation( (int) (screenSize.width - faceSize.getWidth() / 2, (int) (screenSize.height - faceSize.getHeight() / 2);this.setResizable(false);this.setTitle(聊天室服務(wù)端); /設(shè)置標(biāo)題/程序圖標(biāo)icon = getImage(icon.gif);this.setIconImage(icon); /設(shè)置程序圖標(biāo)this.setVisible(true);/* * 程序初始化函數(shù) */public void init()Container contentPane = getContentPane();contentPane.setLayout(new BorderLayout();/添加菜單欄serviceMenu.add (portItem);serviceMenu.add (startItem);serviceMenu.add (stopItem);serviceMenu.add (exitItem);jMenuBar.add (serviceMenu); helpMenu.add (helpItem);jMenuBar.add (helpMenu); setJMenuBar (jMenuBar);/初始化按鈕portSet = new JButton(端口設(shè)置);startServer = new JButton(啟動(dòng)服務(wù));stopServer = new JButton(停止服務(wù) );exitButton = new JButton(退出 );/將按鈕添加到工具欄toolBar.add(portSet);toolBar.addSeparator();/添加分隔欄toolBar.add(startServer);toolBar.add(stopServer);toolBar.addSeparator();/添加分隔欄toolBar.add(exitButton);contentPane.add(toolBar,BorderLayout.NORTH);/初始時(shí),令停止服務(wù)按鈕不可用stopServer.setEnabled(false);stopItem .setEnabled(false);/為菜單欄添加事件監(jiān)聽(tīng)portItem.addActionListener(this);startItem.addActionListener(this);stopItem.addActionListener(this);exitItem.addActionListener(this);helpItem.addActionListener(this);/添加按鈕的事件偵聽(tīng)portSet.addActionListener(this);startServer.addActionListener(this);stopServer.addActionListener(this);exitButton.addActionListener(this);combobox = new JComboBox();combobox.insertItemAt(所有人,0);combobox.setSelectedIndex(0);messageShow = new JTextArea();messageShow.setEditable(false);/添加滾動(dòng)條messageScrollPane = new JScrollPane(messageShow,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);messageScrollPane.setPreferredSize(new Dimension(400,400);messageScrollPane.revalidate();showStatus = new JTextField(35);showStatus.setEditable(false);sysMessage = new JTextField(24);sysMessage.setEnabled(false);sysMessageButton = new JButton();sysMessageButton.setText(發(fā)送);/添加系統(tǒng)消息的事件偵聽(tīng)sysMessage.addActionListener(this);sysMessageButton.addActionListener(this);sendToLabel = new JLabel(發(fā)送至:);messageLabel = new JLabel(發(fā)送消息:);downPanel = new JPanel();girdBag = new GridBagLayout();downPanel.setLayout(girdBag);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 0;girdBagCon.gridy = 0;girdBagCon.gridwidth = 3;girdBagCon.gridheight = 2;girdBagCon.ipadx = 5;girdBagCon.ipady = 5;JLabel none = new JLabel( );girdBag.setConstraints(none,girdBagCon);downPanel.add(none);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 0;girdBagCon.gridy = 2;girdBagCon.insets = new Insets(1,0,0,0);girdBagCon.ipadx = 5;girdBagCon.ipady = 5;girdBag.setConstraints(sendToLabel,girdBagCon);downPanel.add(sendToLabel);girdBagCon = new GridBagConstraints();girdBagCon.gridx =1;girdBagCon.gridy = 2;girdBagCon.anchor = GridBagConstraints.LINE_START;girdBag.setConstraints(combobox,girdBagCon);downPanel.add(combobox);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 0;girdBagCon.gridy = 3;girdBag.setConstraints(messageLabel,girdBagCon);downPanel.add(messageLabel);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 1;girdBagCon.gridy = 3;girdBag.setConstraints(sysMessage,girdBagCon);downPanel.add(sysMessage);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 2;girdBagCon.gridy = 3;girdBag.setConstraints(sysMessageButton,girdBagCon);downPanel.add(sysMessageButton);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 0;girdBagCon.gridy = 4;girdBagCon.gridwidth = 3;girdBag.setConstraints(showStatus,girdBagCon);downPanel.add(showStatus);contentPane.add(messageScrollPane,BorderLayout.CENTER);contentPane.add(downPanel,BorderLayout.SOUTH);/關(guān)閉程序時(shí)的操作this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)stopService();System.exit(0););/* * 事件處理 */public void actionPerformed(ActionEvent e) Object obj = e.getSource();if (obj = startServer | obj = startItem) /啟動(dòng)服務(wù)端startService();else if (obj = stopServer | obj = stopItem) /停止服務(wù)端int j=JOptionPane.showConfirmDialog(this,真的停止服務(wù)嗎?,停止服務(wù),JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);if (j = JOptionPane.YES_OPTION)stopService();else if (obj = portSet | obj = portItem) /端口設(shè)置/調(diào)出端口設(shè)置的對(duì)話框PortConf portConf = new PortConf(this);portConf.setVisible(true);else if (obj = exitButton | obj = exitItem) /退出程序int j=JOptionPane.showConfirmDialog(this,真的要退出嗎?,退出,JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);if (j = JOptionPane.YES_OPTION)stopService();System.exit(0);else if (obj = helpItem) /菜單欄中的幫助/調(diào)出幫助對(duì)話框Help helpDialog = new Help(this);helpDialog.setVisible(true);else if (obj = sysMessage | obj = sysMessageButton) /發(fā)送系統(tǒng)消息sendSystemMessage();/* * 啟動(dòng)服務(wù)端 */public void startService()tryserverSocket = new ServerSocket(port,10);messageShow.append(服務(wù)端已經(jīng)啟動(dòng),在+port+端口偵聽(tīng).n);startServer.setEnabled(false);startItem.setEnabled(false);portSet.setEnabled(false);portItem.setEnabled(false);stopServer .setEnabled(true);stopItem .setEnabled(true);sysMessage.setEnabled(true);catch (Exception e)/System.out.println(e);userLinkList = new UserLinkList();listenThread = new ServerListen(serverSocket,combobox,messageShow,showStatus,userLinkList);listenThread.start();/* * 關(guān)閉服務(wù)端 */public void stopService()try/向所有人發(fā)送服務(wù)器關(guān)閉的消息sendStopToAll();listenThread.isStop = true;serverSocket.close();int count = userLinkList.getCount();int i =0;while( i count)Node node = 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(服務(wù)端已經(jīng)關(guān)閉n);combobox.removeAllItems();combobox.addItem(所有人);catch(Exception e)/System.out.println(e);/* * 向所有人發(fā)送服務(wù)器關(guān)閉的消息 */public void sendStopToAll()int count = userLinkList.getCount();int i = 0;while(i count)Node node = userLinkList.findUser(i);if(node = null) i +;continue;trynode.output.writeObject(服務(wù)關(guān)閉);node.output.flush();catch (Exception e)/System.out.println($+e);i+;/* * 向所有人發(fā)送消息 */public void sendMsgToAll(String msg)int count = userLinkList.getCount();/用戶總數(shù)int i = 0;while(i count)Node node = userLinkList.findUser(i);if(node = null) i +;continue;trynode.output.writeObject(系統(tǒng)信息);node.output.flush();node.output.writeObject(msg);node.output.flush();catch (Exception e)/System.out.println(+e);i+;sysMessage.setText();/* * 向客戶端用戶發(fā)送消息 */public void sendSystemMessage()String toSomebody = combobox.getSelectedItem().toString();String message = sysMessage.getText() + n;messageShow.append(message);/向所有人發(fā)送消息if(toSomebody.equalsIgnoreCase(所有人)sendMsgToAll(message);else/向某個(gè)用戶發(fā)送消息Node node = userLinkList.findUser(toSomebody);trynode.output.writeObject(系統(tǒng)信息);node.output.flush();node.output.writeObject(message);node.output.flush();catch(Exception e)/System.out.println(!+e);sysMessage.setText();/將發(fā)送消息欄的消息清空/* * 通過(guò)給定的文件名獲得圖像 */Image getImage(String file

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論