java版 仿qq代码 完整源码 (已测试)

import javax.swing.Icon;

public class ListItem {

 private String name;
 private Icon icon;
 public ListItem(String name,Icon icon){
  this.name=name;
  this.icon=icon;
 }
 public String getName() {
  return name;
 }
 public Icon getIcon() {
  return icon;
 }
}

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

public class MyFrame {
 private Socket socket = null;
 
 private class MyPane extends JPanel{
  private PrintWriter out = null;
  private BufferedReader in = null;
  private JTextField t;
  private JButton b;
  private DefaultListModel data = new DefaultListModel();
  
  public MyPane(){
   this.setLayout(new BorderLayout());
 
   //list
   JList l = new JList(data);
   JScrollPane sp = new JScrollPane(l);
   this.add(sp, BorderLayout.CENTER);
   
   JPanel p = new JPanel();
   t = new JTextField(20);
   b = new JButton("Send");
   p.add(t);
   p.add(b);
   this.add(p,BorderLayout.SOUTH);
   
   //stream
   try{
    out = new PrintWriter(socket.getOutputStream(),true);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   }catch(Exception e){
    e.printStackTrace();
   }
   //event
   b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
     data.addElement(t.getText());
     out.println(t.getText());
    }
   });
   
   new Thread(new Runnable(){
    public void run(){
     while(true){
      try{
       data.addElement(in.readLine());
      }catch(Exception e){
       return;
      }
     }
    }
   }).start();
  }
 }
 
 public MyFrame(String cs, Socket socket){
  this.socket = socket;
  
  JFrame f = new JFrame();
  f.setTitle(cs + "---" + socket.getInetAddress().getHostAddress());
  f.setLocation(200,300);
  f.setSize(320,200);
  f.getContentPane().add(new MyPane());
  f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  f.setLocationRelativeTo(null);
  f.setVisible(true);
 }
}

 

import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

class MyListRender extends JLabel implements ListCellRenderer {// 列表单元渲染器
 private final Color HIGHLIGHT_COLOR = new Color(130, 160, 220);

 public MyListRender() {
  this.setOpaque(true);
  this.setIconTextGap(5);
 }

 @Override
 public Component getListCellRendererComponent(JList list, Object value,
   int index, boolean isSelected, boolean cellHasFocus) {
  ListItem item = (ListItem) value;// list.add(value);其中的value就是现在的value
  this.setIcon(item.getIcon());
  this.setText(item.getName());
  if (isSelected) {
   this.setBackground(HIGHLIGHT_COLOR);
   this.setForeground(Color.WHITE);
  } else {
   this.setBackground(Color.WHITE);
   this.setForeground(Color.BLACK);
  }
  return this;
 }
}

 

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;

import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;

public class QQMain extends JFrame{
 private static String myname;
 private static String myip=new String();
 private Map<String,String> users=new HashMap<String,String>();
 public QQMain(){
  
  this.add(new MyPanel());
  this.setSize(230,450);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setLocationRelativeTo(null);
  this.setVisible(true);
 }
 public static void run(){
  myname=JOptionPane.showInputDialog("请输入自己的名字").trim();
  if(myname!=null){
   if(myname.equals("")) myname="未知名";
   new QQMain();
  }
 }
 public Map<String,String> getUsers(){
  return users;
 }
 class MyPanel extends JPanel{//主窗口面板列表
  private JList user_list;
  private JButton button;
  private DefaultListModel listmodel;
  private JScrollPane sp;
  private Icon ico=new ImageIcon("img/1.jpg");
  public MyPanel(){
   super();
   initComponent();
  }
  public void initComponent(){
   this.setLayout(new BorderLayout());
   user_list=new JList();
   user_list.setCellRenderer(new MyListRender());
   user_list.setFixedCellHeight(50);
   user_list.addMouseListener(new User_listAction(user_list,users));
   sp=new JScrollPane(user_list);
   
   listmodel=new DefaultListModel();
   user_list.setModel(listmodel);
   listmodel.addElement(new ListItem(myname,ico));
   
   button=new JButton("刷新");
   button.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
     flush();
    }
   });
   this.add(button,BorderLayout.SOUTH);//添加刷新按钮
   this.add(sp);
   
   new Thread(new Runnable(){//增加一个监听用户的线程
    
    InetAddress ip=null;
    DatagramPacket data=null;
    MulticastSocket socket=null;
    String bye="bye";
    byte[] b=null;
    @Override
    public void run() {
     try {
      socket=new MulticastSocket(5000);
      ip=InetAddress.getByName("224.1.1.1");
      socket.joinGroup(ip);
      //发送广播说我上线了
      myip=InetAddress.getLocalHost().getHostAddress().trim();
      socket.send(new DatagramPacket(myname.getBytes(),myname.getBytes().length,ip,5000));
      users.put(myip,myname+" "+myip);
      while(true){//时刻监听是否有用户上线
       b=new byte[50];
       data=new DatagramPacket(b,b.length);
       socket.receive(data);
       String sName=new String(data.getData()).trim();
       String sIp=data.getAddress().getHostAddress().trim();
       if(contain(sIp)){
         //System.out.println(sIp);
         continue;
       }else{
        users.put(sIp, sName+" "+sIp);
        socket.send(new DatagramPacket(myname.getBytes(),myname.getBytes().length,InetAddress.getByName(sIp),5000));
        //System.out.println(users.size());
       }
       flush();
      
      
     } catch (IOException e) {
      e.printStackTrace();
     }finally{
      if(socket!=null){
       socket.close();
      }
     }
    }
    
   }).start();
  }
  public void flush(){
   listmodel.clear();
   Set<String> index=users.keySet();
   Iterator<String> it=index.iterator();
   String s="";
   while(it.hasNext()){
    s=it.next();
    listmodel.addElement(new ListItem(users.get(s),ico));
   }
  }
  public boolean contain(String ip){
   Set<String> user=users.keySet();
   Iterator<String> it=user.iterator();
   String s="";
   while(it.hasNext()){
    s=it.next();
    if(s.trim().equals(ip.trim()))
     return true;
   }
   return false;
  }
 }
}

 

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class QQServer {
 private ServerSocket ss;
 private int port=5500;
 public void start(){
  try {
   ss=new ServerSocket(port);
   while(true){
    Socket incoming = ss.accept();
    new MyFrame("server",incoming);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   if(ss!=null)
    try {
     ss.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
  }
 }
}

 

 

 

 

public class User {
 private String name;
 private String ip;
 public User(String name, String ip) {
  super();
  this.name = name;
  this.ip = ip;
 }
 public String getName() {
  return name;
 }
 public String getIp() {
  return ip;
 }
 }

 

 

 

 

 

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Map;

import javax.swing.JList;
import javax.swing.JOptionPane;

public class User_listAction extends MouseAdapter{
 private JList list;
 private Map<String,String> users;
 private Socket client;
 public User_listAction(JList list, Map<String, String> users) {
  super();
  this.list = list;
  this.users = users;
 }
 public void mouseClicked(MouseEvent e){
  if(e.getClickCount()==2){
   int index=list.getSelectedIndex();
   String[] s=((ListItem)list.getSelectedValue()).getName().split(" ");
   String ip=s[1].trim();
   MyFrame u=null;
   try {
    client=new Socket(ip,5500);
    u=new MyFrame("Client",client);
   }catch (ConnectException e1){
    users.remove(ip);
    JOptionPane.showMessageDialog(null,"该用户已下线!");
   }catch (UnknownHostException e1) {
    users.remove(ip);
    JOptionPane.showMessageDialog(null,"该用户已下线!");
   } catch (IOException e1) {
    e1.printStackTrace();
   }
   
  }
 }
}

 

import javax.swing.JFrame;

public class Main{
 public static void main(String[] args) {
  QQMain.run();
  new QQServer().start();
 }

}

 

 

 

//July、06.11。完成测试。

//编译测试,运行如下:

java版 <wbr>仿qq代码 <wbr>完整源码 <wbr>(已测试)

 

 

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值