JAVA聊天室附代码_第1页
JAVA聊天室附代码_第2页
JAVA聊天室附代码_第3页
JAVA聊天室附代码_第4页
JAVA聊天室附代码_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、. . I . . r . .专业课程设计I基于JAVA的聊天室设计与实现一、课题容和要求基于网络的文本交互软件的设计和实现,设计和实现一个简单的基于网络的文本消息发送和接收软件。根本要求:该软件能实现文本消息发送和接收功能。提高要求(1):支持以多播方式发送文本消息,提高要求2:性能稳定,界面友好。按课程设计报告的要求写出详细的设计和实现报告。二、设计思路分析聊天室是一项进展文本交互的软件,应用广泛,并且实用性强的一个非常典型的人机交互系统。在信息世界里,聊天工具的使用是非常频繁的,如MSN、QQ、校通之类的聊天工具许多人都正在使用。此次设计的聊天室系统主要针对文本信息的传送和接收,不能像Q

2、Q一样进展图片、音频、视频等文件的传输。虽然其功能简单,但是在开发技术和过程中,能够充分学习和使用JAVA的技术,进一步培养了面向对象程序设计的思想,加深对面向对象程序设计四大特性的理解。人们日常生活中越来越多地使用聊天室这项应用来为自己的工作和学习效劳。一个操作简单,界面友好、运行稳定的聊天室对于小型局域网的用户可以起到很好的交流作用。所谓聊天室就是一个可以让许多用户同时互相通信的网上园地,个人用户可以通过聊天室将自己融入整个主流社会中。根据目前使用聊天室的用户需求显示,用户对于聊天室主要有以下几点要求:(1)聊天功能:可以实时接收和发送信息,并能支持公聊和私聊的的方式,能够查看聊天记录,聊

3、天过程中能对字体、表情等进展设定等。(2)用户功能:可以随时注册和登录及注销,并能选择性加好友和删除好友以及查看对方的开放信息,还能看到对方的登录状态。(3)文件传输功能:用户可以共享资源,能发送及接收文件。(4)功能:用户可以设定自己的状态,使*些信息对于其他用户是不可见的。(5)系统稳定:客户端与效劳端的连接稳定,不出现或者少出现连接不上的情况。三、概要设计1、聊天室的简要功能介绍该简易聊天室是为许多喜爱聊天的用户开发设计的,和许多大型的网络聊天室相比该小聊天室功能比拟简单,但能实现根本的聊天功能,点对多、点对点聊天和根本的用户功能,登录、注销、查看在线用户等。(1) 用户设置:功能描述:

4、用户自定义自己的登录名,成功设置后,效劳端能接收到新设置的用户名;(2)登录功能:功能描述:在效劳端开启的情况下,用户可以以自己自定义或者默认的登录名和登录地址端口登录到客户端,并能在效劳器端显示登录状态;(3) 公聊:功能描述:用户在聊天室中可以选择与所有人聊天,聊天室中的其他在线用户可以正常接收并正确显示公聊消息。(4) 私聊:功能描述:用户可以选择与聊天室的在线用户列表中的其他用户聊天。双方的聊天记录对于聊天室的其他用户是不可见的。(5) 聊天心情:功能描述:用户在聊天的过程中可以选择相应的心情来表达自己的情感。(6) 系统消息:功能描述:效劳端正常运行的过程中,在聊天室的每一个用户都能

5、接收效劳器端发送的信息提示信息。 (7)用户的状态.功能描述:每一个客户端用户都可以实时看到目前其他在线用户的状态。(8) 用户注销:功能描述:用户可以注销自己的账号,并重新登记登录名和地址。(9) 用户退出:功能描述:用户可以在正常情况下退出聊天室。(10) 效劳端控制:功能描述:只有在效劳端开启的情况下,用户才能登陆成功和聊天;用户设置的地址端口也必须与效劳端的侦听端口一致,否则不能登录。2、模块描述本聊天室分为聊天室效劳端和聊天室客户端,效劳端与客户端之间通过套接口Socket(TCP)连接。在java中使用套接口相当简单,JavaAPI为处理套接口的通信提供了一个类.Socket.,使

6、得编写网络应用程序相对容易效劳器采用多线程以满足多用户的请求,通过JDBC与后台数据库连接,并通过创立一个ServerSocket对象来监听来自客户的连接请求,默认IP地址为:127.0.0.1默认端口为8888。(1) 聊天室效劳端模块图:聊天室效劳端端口效劳启动效劳发送系统消息停顿效劳退出效劳器消息显示端口效劳:输入要侦听的端口,默认为8888;启动效劳:启动效劳器,并开场在设置的端口中侦听,客户端用户可以登录并开场聊天;停顿效劳:关闭效劳器,侦听完毕。客户端用户不能再聊天;发送系统消息:效劳器端给所有人或者个别用户发送消息。消息显示:可以显示所有用户的聊天记录,和上线、下线信息。退出效劳

7、器:退出程序,并停顿效劳;2聊天室客户端模块图:聊天室客户端连接设置用户登录退出发 送接收聊天消 息用户注销用户设置用户设置:用户可以设置自己的用户名进展聊天;连接设置:用户可以设置自己的效劳器的IP地址和端口号,默认的连接地址为:127.0.0.1:8888;用户登录:只能在聊天室效劳端已开启的情况下,用户才能登录,用户登录之后才能开场聊天,登录后不能再修改用户设置和连接设置;用户注销:退出聊天,可以继续修改用户设置和连接设置;发送和接收聊天信息:用户可以给所有人或者个别用户发送信息,并能看到其他用户发送给自己或所有人的消息;退出:关闭所有聊天并退出客户端。(3) 流程图聊天室效劳器端口效劳

8、启动效劳聊天室客户端登录连接设置聊天退出默认连接地址或用户名设置侦听端口默认端口:8888用户设置设置用户名设置IP地址和端口四、详细设计ActionListenerChatServer-port:public static in-serverSocket:ServerSocket-messageShow:JTe*tArea-userLinkList:UserLinkList-showStatus:JTe*tField+ ChatServer():public+init():public void +startService():public void+stopService():public

9、 void +sendStopToAll():public void+sendMsgToAll(String):public void+ sendSystemMessage():public voidJFrameThreadServerListen- server: ServerSocket- userLinkList :UserLinkList- isStop: public Boolean+ServerListen(ServerSocket,oBo*,JTe*tArea,JTe*tField,UserLinkList)ServerReceive-te*tarea:JTe*tArea- us

10、erLinkList :UserLinkList- isStop: public Boolean+ServerListen(ServerSocket,oBo*,JTe*tArea,JTe*tField,UserLinkList)1、聊天室效劳器端设计:ServerListen- server: ServerSocket- userLinkList :UserLinkList- isStop: public Boolean+ServerListen(ServerSocket,oBo*,JTe*tArea,JTe*tField,UserLinkList)聊天室效劳端主要包括7个文件,它们的功能如下

11、:1ChatServer.java:包含名为ChatServer的public类,其主要功能为定义效劳器端的界面,添加事件侦听与事件处理。调用ServerListen类来实现效劳端用户上线与下线的侦听,调用ServerReceive类来实现效劳器端的消息的收发。2ServerListen.java:该类实现效劳端用户上线与下线的侦听。该类对用户上线下线的侦听是通过调用用户链表类UserLinkList来实现的,当用户上线与下线情况发生变化时,该类会对主类的界面进展相应的修改。3ServerReceive.java:该类是实现效劳器端的消息的收发的类。该类分别定义了向*用户及所有人发送消息的方法

12、,发送的消息会显示在主界面类的界面上。4PortConf.java:该类继承自JDialog,是用户对效劳器端侦听端口进展修改配置的类。5Node.java:用户链表的节点类,定义了链表中的用户。该类与前面所讲的链表节点Node类的功能相当。6UserLinkList.java:用户链表节点的具体实现类。该类通过构造函数构造用户链表,定义了添加用户,删除用户、返回用户数、根据用户名查找用户、根据索引查找用户这5个方法。7Help.java:效劳端程序的帮助类。2、聊天室客户端设计:ActionListenerChatClient-port:public static in-serverSock

13、et:ServerSocket-messageShow:JTe*tArea-userLinkList:UserLinkList-showStatus:JTe*tField+ ChatClient():public+init():public void +Connect():public void+DisConnect():public void +SendMessage():public voidJFrameThreadClientReceive- socket: Socket-output:ObjectOutputStream-input:ObjectInputStream +ClientR

14、eceive(Socket,ObjectOutputStream,ObjectInputStream,oBo*,JTe*tArea,JTe*tField )聊天室客户端主要包括5个文件,它们的功能如下:1ChatClient.java:包含名为ChatClient的public类,其主要功能为定义客户端的界面,添加事件侦听与事件处理。该类定义了Connect()与DisConnect()方法实现与效劳器的连接与断开连接。当登录到指定的效劳器时,调用ClientReceive类实现消息收发,同时该类还定义了SendMessage()方法来向其他用户发送带有表情的消息或者悄悄话。2ClientRe

15、ceive.java:该类是实现效劳器端与客户端消息收发的类。3ConnectConf.java:该类继承自JDialog,是用户对所要连接的效劳器IP及侦听端口进展修改配置的类。4UserConf.java:该类继承自JDialog,是用户对连接到效劳器所显示的用户名进展修改配置的类。5Help.java:客户端程序的帮助类。五、测试数据及其结果分析1、首先运行ChatServer工程文件,会出现如下画面2、选择端口设置,可以设置您要侦听的端口号,不设置将默认端口号为8888;3、选择启动效劳,效劳器开场在您刚刚设置的端口中侦听;4、运行ChatClient工程文件,翻开如下画面5、选择用户

16、设置可以设置自己的登录名6、选择连接设置可以设置您的IP地址和端口号7、选择登录进入聊天室并可以开场跟所有人或者个别其他用户聊天,其中可以选择心情等;8、在效劳端也可以选择给所有人或者个别用户发送消息;注意:客户端只有在效劳端开启的情况下才能正常聊天。9、多人聊天的效劳器端与客户端情况代码如下:package liaotian;/* * 用户链表 */public class UserLinkList Node root;Node pointer;int count;/* * 构造用户链表 */public UserLinkList()root = new Node();root.ne*t =

17、 null;pointer = null;count = 0;/* * 添加用户 */public void addUser(Node n)pointer = root;while(pointer.ne*t != null)pointer = pointer.ne*t;pointer.ne*t = n;n.ne*t = null;count+;/* * 删除用户 */public void delUser(Node n)pointer = root;while(pointer.ne*t != null)if(pointer.ne*t = n)pointer.ne*t = n.ne*t;coun

18、t -;break;pointer = pointer.ne*t;/* * 返回用户数 */public int getCount()return count;/* * 根据用户名查找用户 */public Node findUser(String username)if(count = 0) return null;pointer = root;while(pointer.ne*t != null)pointer = pointer.ne*t;if(pointer.username.equalsIgnoreCase(username)return pointer;return null;/*

19、 * 根据索引查找用户 */public Node findUser(int inde*)if(count = 0) return null;if(inde* 0) return null;pointer = root;int i = 0;while(i inde* + 1)if(pointer.ne*t != null)pointer = pointer.ne*t;elsereturn null;i+;return pointer;package liaotian;import java.awt.*;import java.awt.event.*;import java.io.*;impor

20、t .*;import java*.swing.*;/* * 聊天客户端的主框架类 */public class ChatClient e*tends JFrame implements ActionListener/* * */private static final long serialVersionUID = 1L;String ip = 127.0.0.1;/连接到效劳端的ip地址int port = 8888;/连接到效劳端的端口号String userName = 狂狂;/用户名int type = 0;/0表示未连接,1表示已连接oBo* bobo*;/选择发送消息的承受者JT

21、e*tArea messageShow;/客户端的信息显示JScrollPane messageScrollPane;/信息显示的滚动条JLabel e*press,sendToLabel,messageLabel ;JTe*tField clientMessage;/客户端消息的发送JCheckBo* checkbo*;/悄悄话oBo* actionlist;/表情选择utton clientMessageButton;/发送消息JTe*tField showStatus;/显示用户连接状态Socket socket;ObjectOutputStream output;/网络套接字输出流Ob

22、jectInputStream input;/网络套接字输入流ClientReceive recvThread;/建立菜单栏JMenuBar jMenuBar = new JMenuBar();/建立菜单组JMenu operateMenu = new JMenu (操作(O);/建立菜单项JMenuItem loginItem = new JMenuItem (用户登录(I),new ImageIcon(face/98.gif);JMenuItem logoffItem = new JMenuItem (用户注销(L),new ImageIcon(face/icon.gif);JMenuIt

23、em e*itItem=new JMenuItem (退出(*),new ImageIcon(face/smile.gif);JMenu conMenu=new JMenu (设置(C);JMenuItem userItem=new JMenuItem (用户设置(U),new ImageIcon(face/messenger_big.gif);JMenuItem connectItem=new JMenuItem (连接设置(C),new ImageIcon(face/Uabrand.gif);JMenu helpMenu=new JMenu (帮助(H);JMenuItem helpIte

24、m=new JMenuItem (帮助(H),new ImageIcon(face/HelpCenter.gif);/建立工具栏JToolBar toolBar = new JToolBar();/建立工具栏中的按钮组件utton loginButton;/用户登录utton logoffButton;/用户注销utton userButton;/用户信息的设置utton connectButton;/连接设置utton e*itButton;/退出按钮/框架的大小Dimension faceSize = new Dimension(400, 600);JPanel downPanel ;Gr

25、idBagLayout girdBag;GridBagConstraints girdBagCon;public ChatClient()init();/初始化程序/添加框架的关闭事件处理this.setDefaultCloseOperation(JFrame.E*IT_ON_CLOSE);this.pack();/设置框架的大小this.setSize(faceSize);this.setVisible(true);setIconImage(getToolkit().getImage(face/love.gif);/设置运行时窗口的位置Dimension screenSize = Toolk

26、it.getDefaultToolkit().getScreenSize();this.setLocation( (int) (screenSize.width - faceSize.getWidth() / 2, (int) (screenSize.height - faceSize.getHeight() / 2);this.setResizable(false);this.setTitle(聊天室客户端); /设置标题/为操作菜单栏设置热键VoperateMenu.setMnemonic(O);/为用户登录设置快捷键为ctrl+iloginItem.setMnemonic (I);log

27、inItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_I,InputEvent.CTRL_MASK);/为用户注销快捷键为ctrl+llogoffItem.setMnemonic (L);logoffItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_L,InputEvent.CTRL_MASK);/为退出快捷键为ctrl+*e*itItem.setMnemonic (*);e*itItem.setAccelerator (KeyStroke.getKeyStroke

28、(KeyEvent.VK_*,InputEvent.CTRL_MASK);/为设置菜单栏设置热键CconMenu.setMnemonic(C);/为用户设置设置快捷键为ctrl+uuserItem.setMnemonic (U);userItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_U,InputEvent.CTRL_MASK);/为连接设置设置快捷键为ctrl+cconnectItem.setMnemonic (C);connectItem.setAccelerator (KeyStroke.getKeyStroke (Ke

29、yEvent.VK_C,InputEvent.CTRL_MASK);/为帮助菜单栏设置热键HhelpMenu.setMnemonic(H);/为帮助设置快捷键为ctrl+phelpItem.setMnemonic (H);helpItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_H,InputEvent.CTRL_MASK);/* * 程序初始化函数 */public void init()Container contentPane = getContentPane();contentPane.setLayout(new Bord

30、erLayout();/添加菜单栏operateMenu.add (loginItem);operateMenu.add (logoffItem);operateMenu.addSeparator();operateMenu.add (e*itItem);jMenuBar.add (operateMenu);conMenu.add (userItem);conMenu.addSeparator();conMenu.add (connectItem);jMenuBar.add (conMenu);helpMenu.add (helpItem);jMenuBar.add (helpMenu);se

31、tJMenuBar (jMenuBar);/初始化按钮loginButton = new utton(登录);logoffButton = new utton(注销);userButton = new utton(用户设置 );connectButton = new utton(连接设置 );e*itButton = new utton(退出 );/当鼠标放上显示信息loginButton.setToolTipTe*t(连接到指定的效劳器);logoffButton.setToolTipTe*t(与效劳器断开连接);userButton.setToolTipTe*t(设置用户信息);conne

32、ctButton.setToolTipTe*t(设置所要连接到的效劳器信息);/将按钮添加到工具栏toolBar.add(userButton);toolBar.add(connectButton);toolBar.addSeparator();/添加分隔栏toolBar.add(loginButton);toolBar.add(logoffButton);toolBar.addSeparator();/添加分隔栏toolBar.add(e*itButton);contentPane.add(toolBar,BorderLayout.NORTH);checkbo* = new JCheckBo

33、*(悄悄话);checkbo*.setSelected(false);actionlist = new oBo*();actionlist.addItem(微笑地);actionlist.addItem(快乐地);actionlist.addItem(轻轻地);actionlist.addItem(生气地);actionlist.setSelectedInde*(0);/初始时loginButton.setEnabled(true);logoffButton.setEnabled(false);/为菜单栏添加事件监听loginItem.addActionListener(this);logof

34、fItem.addActionListener(this);e*itItem.addActionListener(this);userItem.addActionListener(this);connectItem.addActionListener(this);helpItem.addActionListener(this);/添加按钮的事件侦听loginButton.addActionListener(this);logoffButton.addActionListener(this);userButton.addActionListener(this);connectButton.add

35、ActionListener(this);e*itButton.addActionListener(this);bobo* = new oBo*();bobo*.insertItemAt(所有人,0);bobo*.setSelectedInde*(0);messageShow = new JTe*tArea();messageShow.setEditable(false);/添加滚动条messageScrollPane = new JScrollPane(messageShow,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZ

36、ONTAL_SCROLLBAR_AS_NEEDED);messageScrollPane.setPreferredSize(new Dimension(400,400);messageScrollPane.revalidate();clientMessage = new JTe*tField(23);clientMessage.setEnabled(false);clientMessageButton = new utton();clientMessageButton.setTe*t(发送);/添加系统消息的事件侦听clientMessage.addActionListener(this);c

37、lientMessageButton.addActionListener(this);sendToLabel = new JLabel(发送至:);e*press = new JLabel( 表情: );messageLabel = new JLabel(发送消息:);downPanel = new JPanel();girdBag = new GridBagLayout();downPanel.setLayout(girdBag);girdBagCon = new GridBagConstraints();girdBagCon.grid* = 0;girdBagCon.gridy = 0;g

38、irdBagCon.gridwidth = 5;girdBagCon.gridheight = 2;girdBagCon.ipad* = 5;girdBagCon.ipady = 5;JLabel none = new JLabel( );girdBag.setConstraints(none,girdBagCon);downPanel.add(none);girdBagCon = new GridBagConstraints();girdBagCon.grid* = 0;girdBagCon.gridy = 2;girdBagCon.insets = new Insets(1,0,0,0);

39、/girdBagCon.ipad* = 5;/girdBagCon.ipady = 5;girdBag.setConstraints(sendToLabel,girdBagCon);downPanel.add(sendToLabel);girdBagCon = new GridBagConstraints();girdBagCon.grid* =1;girdBagCon.gridy = 2;girdBagCon.anchor = GridBagConstraints.LINE_START;girdBag.setConstraints(bobo*,girdBagCon);downPanel.ad

40、d(bobo*);girdBagCon = new GridBagConstraints();girdBagCon.grid* =2;girdBagCon.gridy = 2;girdBagCon.anchor = GridBagConstraints.LINE_END;girdBag.setConstraints(e*press,girdBagCon);downPanel.add(e*press);girdBagCon = new GridBagConstraints();girdBagCon.grid* = 3;girdBagCon.gridy = 2;girdBagCon.anchor

41、= GridBagConstraints.LINE_START;/girdBagCon.insets = new Insets(1,0,0,0);/girdBagCon.ipad* = 5;/girdBagCon.ipady = 5;girdBag.setConstraints(actionlist,girdBagCon);downPanel.add(actionlist);girdBagCon = new GridBagConstraints();girdBagCon.grid* = 4;girdBagCon.gridy = 2;girdBagCon.insets = new Insets(

42、1,0,0,0);/girdBagCon.ipad* = 5;/girdBagCon.ipady = 5;girdBag.setConstraints(checkbo*,girdBagCon);downPanel.add(checkbo*);girdBagCon = new GridBagConstraints();girdBagCon.grid* = 0;girdBagCon.gridy = 3;girdBag.setConstraints(messageLabel,girdBagCon);downPanel.add(messageLabel);girdBagCon = new GridBa

43、gConstraints();girdBagCon.grid* = 1;girdBagCon.gridy = 3;girdBagCon.gridwidth = 3;girdBagCon.gridheight = 1;girdBag.setConstraints(clientMessage,girdBagCon);downPanel.add(clientMessage);girdBagCon = new GridBagConstraints();girdBagCon.grid* = 4;girdBagCon.gridy = 3;girdBag.setConstraints(clientMessa

44、geButton,girdBagCon);downPanel.add(clientMessageButton);showStatus = new JTe*tField(35);showStatus.setEditable(false);girdBagCon = new GridBagConstraints();girdBagCon.grid* = 0;girdBagCon.gridy = 5;girdBagCon.gridwidth = 5;girdBag.setConstraints(showStatus,girdBagCon);downPanel.add(showStatus);conte

45、ntPane.add(messageScrollPane,BorderLayout.CENTER);contentPane.add(downPanel,BorderLayout.SOUTH);/关闭程序时的操作this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)if(type = 1)DisConnect();System.e*it(0););/* * 事件处理 */public void actionPerformed(ActionEvent e) Object obj = e.g

46、etSource();if (obj = userItem | obj = userButton) /用户信息设置/调出用户信息设置对话框UserConf userConf = new UserConf(this,userName);userConf.setVisible(true);userName = userConf.userInputName;else if (obj = connectItem | obj = connectButton) /连接效劳端设置/调出连接设置对话框ConnectConf conConf = new ConnectConf(this,ip,port);con

47、Conf.setVisible(true);ip = conConf.userInputIp;port = conConf.userInputPort;else if (obj = loginItem | obj = loginButton) /登录Connect();else if (obj = logoffItem | obj = logoffButton) /注销DisConnect();showStatus.setTe*t();else if (obj = clientMessage | obj = clientMessageButton) /发送消息SendMessage();cli

48、entMessage.setTe*t();else if (obj = e*itButton | obj = e*itItem) /退出int j=JOptionPane.showConfirmDialog(this,真的要退出吗,退出,JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);if (j = JOptionPane.YES_OPTION)if(type = 1)DisConnect();System.e*it(0);else if (obj = helpItem) /菜单栏中的帮助/调出帮助对话框Help helpDialog

49、= new Help(this);helpDialog.setVisible(true);public void Connect()trysocket = new Socket(ip,port);catch (E*ception e)JOptionPane.showConfirmDialog(this,不能连接到指定的效劳器。n请确认连接设置是否正确。,提示,JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);return;tryoutput = new ObjectOutputStream(socket.getOutputStrea

50、m();output.flush();input = new ObjectInputStream(socket.getInputStream() );output.writeObject(userName);output.flush();recvThread = new ClientReceive(socket,output,input,bobo*,messageShow,showStatus);recvThread.start();loginButton.setEnabled(false);loginItem.setEnabled(false);userButton.setEnabled(f

51、alse);userItem.setEnabled(false);connectButton.setEnabled(false);connectItem.setEnabled(false);logoffButton.setEnabled(true);logoffItem.setEnabled(true);clientMessage.setEnabled(true);messageShow.append(连接效劳器 +ip+:+port+ 成功.n);type = 1;/标志位设为已连接catch (E*ception e)System.out.println(e);return;public

52、void DisConnect()loginButton.setEnabled(true);loginItem.setEnabled(true);userButton.setEnabled(true);userItem.setEnabled(true);connectButton.setEnabled(true);connectItem.setEnabled(true);logoffButton.setEnabled(false);logoffItem.setEnabled(false);clientMessage.setEnabled(false);if(socket.isClosed()r

53、eturn ;tryoutput.writeObject(用户下线);output.flush();input.close();output.close();socket.close();messageShow.append(已经与效劳器断开连接.n);type = 0;/标志位设为未连接catch (E*ception e)/public void SendMessage()String toSomebody = bobo*.getSelectedItem().toString();String status = ;if(checkbo*.isSelected()status = 悄悄话;S

54、tring action = actionlist.getSelectedItem().toString();String message = clientMessage.getTe*t();if(socket.isClosed()return ;tryoutput.writeObject(聊天信息);output.flush();output.writeObject(toSomebody);output.flush();output.writeObject(action);output.flush();output.writeObject(action);output.flush();out

55、put.writeObject(message);output.flush();catch (E*ception e)/public static void main(String args) new ChatClient();package liaotian;import java.awt.*;import java.awt.event.*;import .*;import java*.swing.*;public class ChatServer e*tends JFrame implements ActionListenerpublic static int port=8888;/Ser

56、verSocket serverSocket;/SocketoBo* bobo*;JTe*tArea messageShow;JTe*tField showStatus;JScrollPane messageScrollPane;JLabel sendToLabel,messageLabel;JTe*tField sysMessage;utton sysMessageButton;UserLinkList userLinkList;JMenuBar jMenuBar=new JMenuBar();JMenu serviceMenu = new JMenu();JMenuItem portIte

57、m = new JMenuItem();JMenuItem startItem = new JMenuItem();JMenuItem stopItem = new JMenuItem();JMenuItem e*itItem = new JMenuItem( );JMenu helpMenu = new JMenu();JMenuItem helpItem = new JMenuItem();JToolBar toolBar = new JToolBar();utton portSet;utton startServer;utton stopServer;utton e*itButton;/

58、Dimension faceSize = new Dimension(400,600);ServerListen listenThread;JPanel downPanel;GridBagLayout girdBag;GridBagConstraints girdBagCon;/* * */public ChatServer()init();/this.setDefaultCloseOperation(JFrame.E*IT_ON_CLOSE);this.pack();this.setSize(faceSize);this.setVisible(true);/Dimension screenS

59、ize = Toolkit.getDefaultToolkit().getScreenSize();this.setLocation(int)(screenSize.width-faceSize.getWidth()/2, (int)(screenSize.height-faceSize.getHeight()/2);this.setResizable(false);this.setTitle();public void init()Container contentPane = getContentPane();contentPane.setLayout(new BorderLayout()

60、;serviceMenu.add(portItem);serviceMenu.add(startItem);serviceMenu.addSeparator();serviceMenu.add(stopItem);serviceMenu.addSeparator();serviceMenu.add(e*itItem);jMenuBar.add(serviceMenu);helpMenu.add(helpItem);jMenuBar.add(helpMenu);setJMenuBar(jMenuBar);/portSet = new utton();startServer = new utton

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论