一个简单的java聊天室程序功能齐全_第1页
一个简单的java聊天室程序功能齐全_第2页
一个简单的java聊天室程序功能齐全_第3页
一个简单的java聊天室程序功能齐全_第4页
一个简单的java聊天室程序功能齐全_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

聊天室使用说明1、 开发语言:Java高级语言编程2、 开发环境:JCreator Pro3、 运行环境:JCreator Pro4、 使用流程:首先在JCreator Pro环境中编译,然后打开运行即可。效果图浏览:u 服务器端代码import java.io.*;import java.awt.*;import java.awt.event.*;import .*;import java.util.Collection;import java.util.Hashtable;import java.util.Iterator;public class ChatServer extends Frame TextArea ta=new TextArea(20,52);/消息接收显示框 TextField tf=new TextField(32);/消息编写框 Button b=new Button( 发 送 );/发送消息按钮 Button start=new Button(启动服务); ServerSocket server=null; TextField portInput=new TextField(4444,2); List userList=new List(21); Button refreshUser=new Button( 刷 新 用 户 列 表 ); /Vector users=new Vector(); Hashtable hash=new Hashtable(); public ChatServer() super(丁丁猫聊天室服务器端); ta.setBackground(new Color(100,190,50);/设置背景色 ta.setForeground(Color.red);/设置字体颜色 ta.setEditable(false);/文本框不可编辑 /this.add(ta,BorderLayout.NORTH); Panel p=new Panel(new FlowLayout(FlowLayout.LEFT);/第一个Panel,放置输入数据 p.add(new Label(Port); p.add(portInput); p.add(start); p.add(tf); p.add(b); Panel pleft=new Panel(new BorderLayout();/第二个Panel,显示登陆数据 pleft.add(ta,BorderLayout.NORTH); pleft.add(p,BorderLayout.CENTER); Panel pright=new Panel(new BorderLayout(); userList.setBackground(new Color(10,160,90); userList.setForeground(Color.orange); Panel pb=new Panel();/第三个Panel,显示客户端输入数据 pb.add(refreshUser); pright.add(pb,BorderLayout.CENTER); pright.add(userList,BorderLayout.NORTH); /this.add(p,BorderLayout.CENTER); this.add(pleft,BorderLayout.CENTER); this.add(pright,BorderLayout.EAST); this.setResizable(true);/能调节窗口大小 MyActionListener lis=new MyActionListener(); this.addWindowListener(lis); start.addActionListener(lis); b.addActionListener(lis); refreshUser.addActionListener(lis); this.pack(); this.show(); public static void main(String args) ChatServer s = new ChatServer(); public void showUserList() userList.clear(); Collection col=hash.keySet(); Iterator it=col.iterator(); int i=0; while(it.hasNext() String name=(String)it.next(); userList.add(name); i+; this.setTitle(丁丁猫聊天室服务器端-当前在线+i+位用户); /广播消息 public void broadCast(String msg) Collection col=hash.values(); Iterator it=col.iterator(); while(it.hasNext() try Socket c = (Socket) it.next(); OutputStream os = c.getOutputStream(); PrintStream ps = new PrintStream(os); ps.println(msg); catch (IOException ex) ex.printStackTrace(); /内部类,做事件处理 class MyActionListener extends WindowAdapter implements ActionListener Service service = new Service(); public void actionPerformed(ActionEvent e) String cmd = e.getActionCommand(); if(cmd.equals(启动服务) String tmp=null; try if(server!=null&!server.isClosed() server.close(); server = new ServerSocket(Integer.parseInt(portInput.getText(); service.start(); tmp=系统消息:服务器启动成功n; catch (Exception ex) tmp=系统消息:服务器启动失败n; /ex.printStackTrace(); ta.setText(tmp); if(cmd.equals( 发 送 ) broadCast(主人:+tf.getText(); ta.append(主人:+tf.getText()+n); if(cmd.equals( 刷 新 用 户 列 表 ) showUserList(); public void windowClosing(WindowEvent e) System.exit(0);/关闭窗口,退出程序 /接受连接客户端 class Service extends Thread public void run() while (true) try Socket client = server.accept(); ServiceUnit unit=new ServiceUnit(client); unit.start(); Thread.sleep(100); catch (Exception ex) /ex.printStackTrace(); break; /接受客户端聊天信息 class ServiceUnit extends Thread Socket client = null; InputStream is = null; BufferedReader br = null; String msg = null; String returnMsg=null; int index=0; String name=null; public ServiceUnit(Socket c) client=c; public void run() /users.add(client); while (true) try is = client.getInputStream(); br = new BufferedReader(new InputStreamReader(is); msg = null; while (msg = br.readLine() != null) index+; if (index = 1) name = msg+-+client.getInetAddress().getHostAddress(); ta.append( + name + 进入聊天室n); returnMsg = 主人:欢迎 + name + 进入聊天室; /把登陆的用户和信息封装到一个hashtable(K,V)里 hash.put(name,client); showUserList(); else ta.append( + name + : + msg + n); returnMsg = + name + : + msg; broadCast(returnMsg); Thread.sleep(100); catch (Exception ex) /ex.printStackTrace(); break; hash.remove(name); broadCast( + name + 离线); ta.append( + name + 离线n); showUserList(); u 客户端代码import java.io.*;import java.awt.*;import java.awt.event.*;import .*;public class ChatClient extends Frame TextArea ta=new TextArea(10,40);/消息接收显示框 TextField tf=new TextField(20);/消息编写框 TextField ipInput=new TextField(localhost,10);/IP输入框 TextField portInput=new TextField(4444,2);/输入端口 TextField nameInput=new TextField(user,5);/输入端口 Button conn=new Button(登录); Button b=new Button(发送);/发送消息按钮 Socket client=null; public ChatClient() super(丁丁猫聊天室客户端); ta.setBackground(new Color(110,150,120); ta.setForeground(Color.blue); ta.setEditable(false); this.add(ta,BorderLayout.NORTH); Panel p=new Panel(); p.add(new Label(IP); p.add(ipInput); p.add(new Label(Port); p.add(portInput); p.add(new Label(Name); p.add(nameInput); p.add(conn); p.add(tf); p.add(b); this.add(p,BorderLayout.CENTER); this.setResizable(true);/不能调节窗口大小 MyActionListener lis=new MyActionListener(); this.addWindowListener(lis); conn.addActionListener(lis);/给登录按钮添加事件处理器 b.addActionListener(lis); this.pack(); this.show(); public static void main(String args) ChatClient cc = new ChatClient(); /内部类,做事件处理 class MyActionListener extends WindowAdapter implements ActionListener public void actionPerformed(ActionEvent e) String cmd=e.getActionCommand(); if(cmd.equals(登录) String tmp=null; try /创建到指定服务器的socket连接 client = new Socket(ipInput.getText(),Integer.parseInt(portInput.getText(); tmp=系统消息:连接成功n; ReadMsg reader=new ReadMsg(); reader.start(); catch (Exception ex) tmp=系统消息:连接失败n; ta.setText(tmp);/显示登录成功或失败信息 if(cmd.equals(发送) OutputStream os =null; PrintStream ps =null; try os = client.getOutputStream(); ps = new PrintStream(os); ps.println(tf.getText(); catch (Exception ex1) /ex1.printStackTra

温馨提示

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

评论

0/150

提交评论