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

📄 client.java

📁 一个用java写的一种聊天工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package Form;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.imageio.ImageIO;
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.JTree;
import javax.swing.SwingConstants;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;

import DataLayer.*;
import business.*;

public class Client extends JFrame{

	private SqlHelper sh;
	private int userNum;
	private int friendNum;
	private Vector friend;  //存储好友信息列表
	private Vector group;   //存储群组信息
	private Hashtable recent = new Hashtable();
	private Vector friendList = new Vector(); //存放打卡的聊天的窗口
	private Hashtable ht_friend = new Hashtable(); //获取打开的对话框
	private Hashtable ht_Group = new Hashtable();//存放打开群窗口
	
	private boolean doubleClick = false;
	private boolean login = true;
	private String rightNow;
	private InetAddress clientAddress;
	private Socket connection;
	private DataInputStream in;
	private DataOutputStream out;
	private String strIP;
	private int port = 3000;
	private recThread rec;
	
	private JPanel panel;
	private JPanel pnlGroup;   //用于显示qq群列表
	private JScrollPane scrollPane; //用于显示好友列表
	private JPanel pnlRecent;  //用于显示最近联系人
	private JButton button;   //我的好友
	private JButton btnFriend; //Myqq群
	private JButton btnRecent; //最近联系人
	private JButton btnFind;   //查找按钮

	/**
	 * Create the frame
	 * @throws SQLException 
	 */
	//关闭按钮触发事件
	private class WindowCloser extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			Client.this.clientClosed();
		}
	}
	private void clientClosed()
	{
		try {
			out.writeUTF("Q");
		} catch (IOException e) {
		}
		finally
		{
			User.setOnLine(0, userNum);
			System.exit(0);
		}
	}
	
	//用于显示聊天对话框
	private void showChat(int friendNum)
	{
		//int lastIndex = 0;
		//System.out.println(friendNum);
		Enumeration en = friendList.elements();
		while(en.hasMoreElements())
		{
			String temp = (String)en.nextElement();
			//lastIndex++;
			if(temp.equals(""+friendNum))
			{
				return;
			}
		}
		friendList.add(friendNum+"");
		
		Chat c = new Chat(Client.this.userNum,friendNum,Client.this);
		c.setVisible(true);
		ht_friend.put(friendNum+"", c);
	}

	private void showChatGroup(int groupID)
	{
		Enumeration en = ht_Group.elements();
		while(en.hasMoreElements())
		{
			ChatGroup temp = null;
			try
			{
			    temp = (ChatGroup)ht_Group.get(groupID+"");
			}
			catch(Exception e)
			{
			}
			if(temp!=null)
			{
				return;
			}
			break;	
		}
		ChatGroup cg = new ChatGroup(userNum,groupID,Client.this);
		ht_Group.put(groupID+"", cg);
		cg.setVisible(true);
	}
	//用于为绑定好友信息

	private void setFriendList(JPanel panel) throws SQLException
	{
		int top = 5;
		int left = 5;
		int width = 190;
		int height = 60;
		int increment = 60;
		int size = 0;
		String logoStr = null;
		String nameStr = null;
		String info = null;
		
		//绑定自己的个人信息
		ResultSet rs = sh.getQuery("select userLogo,userName,userInformation from tbl_Users where userNum="+ userNum);
		if(rs.next())
		{
			logoStr = "image\\"+rs.getInt(1)+".jpg";
			nameStr = rs.getString(2);
			info = rs.getString(3);
		}
		ImageIcon logo = new ImageIcon(logoStr);
		final JButton btnLogo = new JButton(logo);
		btnLogo.setBounds(5, 5, 50, 50);
		getContentPane().add(btnLogo);
		btnLogo.addMouseListener(new MouseAdapter(){
			Info userInfo =null ;
			public void mouseEntered(MouseEvent e) {
				// TODO 自动生成方法存根
				userInfo = new Info(Client.this,"",false,Client.this.userNum);
				userInfo.setVisible(true);
			}
		});
		
		final JLabel lblMy = new JLabel(userNum+" 在线");
		lblMy.setBounds(70, 8, 100, 20);
		getContentPane().add(lblMy);
		final JLabel lblInfo = new JLabel(nameStr);
		lblInfo.setBounds(70, 20, 100, 40);
		getContentPane().add(lblInfo);
		
		//为好友信息做数据绑定
	    rs = sh.getQuery("select friendNum from tbl_Friends where userNum="+ userNum);
	   
	    while(rs.next())
	    {
	    	int friendNum = rs.getInt(1);
	    	User ufriend = User.getUser("select * from tbl_Users where userNum="+ friendNum);
	    	friend.add(ufriend);
	    }
	    Collections.sort(friend,new User());
	    Enumeration en = friend.elements();
	    while(en.hasMoreElements())
	    {
	    	User fUser = (User)en.nextElement();
	    	//System.out.println(fUser.isOnLine);
	    	if(fUser.isOnLine==1)
	    	{
	    		logoStr = "image\\"+fUser.userLogo+".jpg";
	    	}
	    	else
	    	{
	    		logoStr = "image\\copy\\"+fUser.userLogo+".jpg";
	    	}
	    	nameStr = fUser.userName;
	    	info = fUser.userInformation;
	    	friendNum = fUser.userNum;
	 
	    	
	    	final JButton bf = new JButton();
			JLabel lblfName = new JLabel();
			JLabel lblfInfo = new JLabel();
			final JPanel pnlFriend = new JPanel();
			pnlFriend.addMouseListener(new MouseAdapter(){
				//鼠标进入的事件
				public void mouseEntered(MouseEvent e) {
					// TODO 自动生成方法存根
					pnlFriend.setBackground(new Color(221,232,243));
				}
				//鼠标移出的事件
				public void mouseExited(MouseEvent e) {
					// TODO 自动生成方法存根
					pnlFriend.setBackground(Color.WHITE);
				}
			});
			pnlFriend.setLayout(null);
			pnlFriend.setBackground(Color.WHITE);
			bf.setBounds(5, 10, 50, 50);
			lblfName.setBounds(60, 10, 100, 20);
			lblfInfo.setBounds(60, 30, 150, 20);
			pnlFriend.add(bf);
			pnlFriend.add(lblfName);
			pnlFriend.add(lblfInfo);
			
	    	logo = new ImageIcon(logoStr);
	    	bf.setIcon(logo);
	    	bf.setActionCommand(friendNum+"");
	    	bf.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					// TODO 自动生成方法存根
					if(doubleClick)
					{
						int friendNum = new Integer(e.getActionCommand()).intValue();
						Client.this.showChat(friendNum);//调用显示聊天对话框
					}
					doubleClick = false;
				}
	    	});
	    	bf.addMouseListener(new MouseAdapter() {
	    	Info infoUser = null;
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount()>=2)
				{
					doubleClick = true;
				}
			}

			public void mouseEntered(MouseEvent e) {
				// TODO 自动生成方法存根
				infoUser =  new Info(Client.this,"",false,new Integer(bf.getActionCommand()).intValue());
				infoUser.setVisible(true);
			}

			public void mouseExited(MouseEvent e) {
				// TODO 自动生成方法存根
				infoUser.dispose();
			}
	    	});
	    	lblfName.setText(nameStr);
	    	lblfInfo.setText(info);
	    	panel.add(pnlFriend);
	    	pnlFriend.setBounds(left, top, width, height);
	    	top = top + increment;
	    	size = size + increment;
	    }
	    Dimension area = new Dimension(); 
		area.height=size;
		area.width=200;
		panel.setPreferredSize(area);
	}
	
	//用于界面模块见的切换
	private void switchTab(final int tag)
	{
		if(scrollPane.isVisible())
		{
			final Timer t = new Timer("ss");
			t.schedule(new TimerTask(){
				private int i = 1 ;
				public void run()
				{
					if(i<=10)
					{
						int height = 200- 20*i;
						int top = 280-20*i;
						scrollPane.setSize(200, height);
						btnFriend.setBounds(0, top, 200, 20);
						if(tag==2)
						{
							btnRecent.setBounds(0, top+20, 200, 20);
						}
						i++;
					}
					else
					{
						scrollPane.setVisible(false);
						t.cancel();
					}
				}
			}, 0, 10);
		}
		else if(pnlGroup.isVisible())
		{
			final Timer t = new Timer("ss");
			t.schedule(new TimerTask(){
				private int i = 1 ;
				public void run()
				{
					if(i<=10)
					{
						int height = 200- 20*i;
						int top = 300-20*i;
						pnlGroup.setSize(200, height);
						btnRecent.setBounds(0, top, 200, 20);
						i++;
					}
					else
					{
						pnlGroup.setVisible(false);
						t.cancel();
					}
				}
			}, 0, 10);
		}
		else
		{
			pnlRecent.setVisible(false);
		}
	}
	
	//为最近联系人绑定事件
	private void setRecent()
	{
		int top = 10 ;
		pnlRecent.removeAll();
		for(Enumeration en = recent.elements(); en.hasMoreElements();){
			User user= (User)en.nextElement();
			String logoStr = "";
			int isOnlien = User.getIsOnLine(user.userNum);
			JButton temp = new JButton();
			temp.setBounds(75, top, 50, 50);
			if(isOnlien==1)
	    	{
	    		logoStr = "image\\"+user.userLogo+".jpg";
	    	}
	    	else
	    	{
	    		logoStr = "image\\copy\\"+user.userLogo+".jpg";
	    	}
			temp.setIcon(new ImageIcon(logoStr));
			temp.setActionCommand(user.userNum+"");
			top +=55;
			temp.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					int friendNum = new Integer(e.getActionCommand()).intValue();
					Client.this.showChat(friendNum);
				}
				
			});
			pnlRecent.add(temp);
		}
		this.getContentPane().add(pnlRecent);
		pnlRecent.updateUI();
	}
	
	//用于绑定QQ群信息 
	private void setGroup(JPanel panel) throws SQLException
	{
		int index = 0;
		int increment = 20;
		ResultSet rs = sh.getQuery("select groupID from tbl_GroupList where userNum="+ userNum);
		JButton jbArray[] =new JButton[16];
		while(rs.next())
		{
			int groupID = rs.getInt(1);
			Group temp = Group.getGroup(groupID);
			group.add(temp);
		}
		Enumeration en = group.elements();
		while(en.hasMoreElements())
		{
			final Group temp = (Group)en.nextElement();
		    jbArray[index] = new JButton();
		    jbArray[index].setBackground(Color.WHITE);
		    jbArray[index].setIcon(new ImageIcon("image\\qun.jpg"));
		    jbArray[index].setText(temp.groupName);
		    jbArray[index].setHorizontalAlignment(SwingConstants.LEFT);
		    jbArray[index].addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					Client.this.showChatGroup(temp.groupID);
				}
		    	
		    });
			index++;
		}
		for(int i=0;i<index;i++)
		{
			jbArray[i].setBounds(0,1+23*i, 200, 20);
			panel.add(jbArray[i]);
		}
		group.removeAllElements();
	}
	
//	用于初始化窗体的控件
	private void setup()
	{
		button = new JButton();

⌨️ 快捷键说明

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