1. 程式人生 > >java版 仿qq程式碼 完整原始碼 (已測試)

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();