⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cxzc.java

📁 这是一个仿QQ的聊天程序
💻 JAVA
字号:
package com.cloudcloud.soft;

import java.awt.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.*;

public class cxzc extends JFrame implements Runnable,WindowListener
{
	JLabel lHeadPho;
	JLabel lUserNum;
	JPanel jUserStatus;
	JPanel jFriends;
	Socket s;
	Hashtable ht = new Hashtable();
	static int frames = 0;
	cawesda me;
	
	public void setSocket(Socket s)
	{
		this.s = s;
	}
	cxzc(cawesda me)
	{
		this.me = me;
		this.addWindowListener(this);
		this.setTitle("CloudQQ");
		this.setBounds(100,100,200,400);
		this.setLayout(new BorderLayout());
		
		lHeadPho = new JLabel();
		lHeadPho.setIcon(new ImageIcon(getImageSource("face/" + me.getHeadNum() + ".gif")));
		
		lUserNum = new JLabel();
		lUserNum.setFont(new Font("宋体", Font.PLAIN, 12));
		lUserNum.setText("  " + me.getName() + "  (" + me.getID() + ")");
		
		jUserStatus = new JPanel();
		jUserStatus.setLayout(new BorderLayout());
		jUserStatus.add(lHeadPho, BorderLayout.WEST);
		jUserStatus.add(lUserNum, BorderLayout.CENTER);
		
		jFriends = new JPanel();
		jFriends.setBackground(Color.WHITE);
		jFriends.setLayout(new GridLayout(6,1));
		
		this.add(jFriends, BorderLayout.CENTER);
		this.add(jUserStatus, BorderLayout.NORTH);
		
		Thread t = new Thread(this);
		t.start();
	}
	private BufferedImage getImageSource(String imagePath)
	{
		BufferedImage image = null;
		InputStream in = Class.class.getClass().getResourceAsStream("/" + imagePath);
		try 
		{
			image = ImageIO.read(in);
			return image;
		}
		catch (IOException e) 
		{
			e.printStackTrace();
			return image;
		}
	}
	public void run()
	{
		String sep = "==QQ==";
		cawesda m;
		while(true)
		{
			Zdasdasd ms = new Zdasdasd(s);
			String mass = ms.readLine();

			if(mass.split("==com==")[0].equals("USERS"))
			{
				String userData = mass.split("==com==")[1];
				m = new cawesda();
				m.setID(Integer.parseInt(userData.split(sep)[0]));
				m.setPwd(userData.split(sep)[1]);
				m.setName(userData.split(sep)[2]);
				m.setHeadNum(Integer.parseInt(userData.split(sep)[3]));
				m.setSex(userData.split(sep)[4]);
				m.setAge(Integer.parseInt(userData.split(sep)[5]));
				m.setEmail(userData.split(sep)[6]);
				m.setShow(userData.split(sep)[7]);
				MyFriendPanel jf = new MyFriendPanel(me, m);
				jf.setSocket(s);
				jFriends.add(jf);
				ht.put(Integer.toString(m.getID()),jf);
			}
			else if(mass.split("==com==")[0].equals("CHAT"))
			{
				String chatInfo = mass.split("==com==")[1];
				String fromID = chatInfo.split("==QQ==")[0];
				String chatAndStyle = chatInfo.split("==QQ==")[2];
				MyFriendPanel jf = (MyFriendPanel)ht.get(fromID);
				jf.dealChat(chatAndStyle);
			}
			else if(mass.split("==com==")[0].equals("REMOVE_USERS"))
			{
				String fromID = mass.split("==com==")[1];
				MyFriendPanel jf = (MyFriendPanel)ht.get(fromID);
				jFriends.remove(jf);
				jFriends.repaint();
				ht.remove(fromID);
			}
			
			this.setVisible(true);

		}
	}
	
	
	
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void windowClosing(WindowEvent e) 
	{
		Zdasdasd ms = new Zdasdasd(s);
		ms.println("LOGOUT==com==" + me.getID());
		try 
		{
			s.close();
		}
		catch (IOException e1)
		{
			e1.printStackTrace();
		}
		s = null;
		System.exit(0);
		
	}
	public void windowClosed(WindowEvent e)
	{
		
	}
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

}

class MyFriendPanel extends JPanel implements MouseListener
{
	JLabel l1;
	JLabel l2;
	JLabel lBG;
	cawesda m;
	cawesda me;
	daswdasd c;
	ShakeHead sh;
	String allChatAndStyle = "";
	Socket s;
	public void setSocket(Socket s)
	{
		this.s = s;
	}
	MyFriendPanel(cawesda me, cawesda m)
	{
		this.me = me;
		this.m = m;
		this.setLayout(new BorderLayout());
		this.setBackground(Color.WHITE);
		this.setLayout(null);
		l1 = new JLabel();
		l1.setIcon(new ImageIcon(getImageSource("face/" + m.getHeadNum() + ".gif")));
		l1.setBounds(13,10,40,40);
		l2 = new JLabel();
		l2.setBounds(60,10,150,50);
		l2.setFont(new Font("", Font.PLAIN, 12));
		l2.setText("     " + m.getName());
		this.add(l1);
		this.add(l2);
		this.addMouseListener(this);
	}
	private BufferedImage getImageSource(String imagePath)
	{
		BufferedImage image = null;
		InputStream in = Class.class.getClass().getResourceAsStream("/" + imagePath);
		try 
		{
			image = ImageIO.read(in);
			return image;
		}
		catch (IOException e) 
		{
			e.printStackTrace();
			return image;
		}
	}
	public void setOtherSideLeave()
	{
		if(c == null || c.isVisible() == false)
		{
		}
		else
		{
			c.setOtherSideLeave();
		}
	}
	public void dealChat(String chatAndStyle)
	{
		if(c == null || c.isVisible() == false)
		{
			System.out.println(c);
			if(sh == null)
			{
				sh = new ShakeHead(l1);
				sh.start();
			}
			allChatAndStyle  = chatAndStyle;
		}
		else
		{
			c.dealChat(chatAndStyle);
		}
	}
	public void mouseClicked(MouseEvent e) 
	{
		if(e.getClickCount() == 2)
		{ 
			if(sh != null)
			{
				sh.stopShake();
				sh = null;
			}
			c = new daswdasd(me, m);
			c.setSocket(s);
			c.setL(cxzc.frames++);
			if(!allChatAndStyle.equals(""))
			{
				c.dealChat(allChatAndStyle);
			}
			c.setVisible(true);
		}
	}
	public void mousePressed(MouseEvent e) {
		
	}
	public void mouseReleased(MouseEvent e) {
		
	}
	public void mouseEntered(MouseEvent e) {
		
	}
	public void mouseExited(MouseEvent e) {
		
	}
}


class ShakeHead extends Thread
{
	JLabel l;
	boolean isShake = true;
	public void stopShake()
	{
		isShake = false;
	}
	ShakeHead(JLabel l)
	{
		this.l = l;
	}
	public void run()
	{
		while(isShake)
		{
			l.setLocation(10,13);
			try 
			{
				Thread.sleep(300) ;
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			l.setLocation(13,10);
			try 
			{
				Thread.sleep(300) ;
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			l.setLocation(16,13);
			try 
			{
				Thread.sleep(300) ;
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			l.setLocation(13,10);
			try 
			{
				Thread.sleep(300) ;
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -