首页>代码>java swing仿qq写的群聊软件>/javaqq/src/ClientUtil.java
import java.awt.Color;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class ClientUtil {
	/**
	 * 处理已接收信息,将表情与字符分开
	 */
	public static ArrayList dealmessage(String message) {
		String msg = message;
		// 处理字符串

		ArrayList list = new ArrayList();
		ArrayList substrlist = new ArrayList();
		list.add(0);

		Pattern p = Pattern.compile(".gif");
		Matcher m = p.matcher(msg);
		while (m.find()) {
			int start = m.start() - 2;
			list.add(start);
			start = start + 6;
			if (start > msg.length()) {
				break;
			}
			list.add(start);
		}
		
		for (int i = 0; i < list.size(); i++) {
			if (i < list.size()-1) {
				int begin = Integer.parseInt(list.get(i).toString());
				int over = Integer.parseInt(list.get(i + 1).toString());
				String subs = msg.substring(begin, over);
				substrlist.add(subs);
			}else{
				String subs = msg.substring(Integer.parseInt(list.get(i).toString()));
				substrlist.add(subs);
			}
		}
		return substrlist;
	}
	
	/**
	 * 将处理过的信息显示在聊天面板上
	 */
	public void printmsg(ArrayList list,JTextPane tPane,Message u){
		Icon image = null;
		JTextPane textPane = tPane;
		StyledDocument doc = textPane.getStyledDocument();
		SimpleAttributeSet attr = new SimpleAttributeSet();
		StyleConstants.setForeground(attr, Color.red);
		ArrayList msglist = list;
		Pattern p = Pattern.compile(".gif");
		try {
			doc.insertString(doc.getLength(), u.toString() ,
					attr);
		} catch (BadLocationException e1) {
			e1.printStackTrace();
		}
		for (int i = 0; i < msglist.size(); i++) {
			String sub = msglist.get(i).toString();
			Matcher m = p.matcher(sub);
			if(m.find()){
				URL url = this.getClass().getClassLoader().getResource("Image/" + sub);
				textPane.setCaretPosition(doc.getLength()); // 设置插入位置
				image = new ImageIcon(url);
				textPane.insertIcon(image);
			}else{
				try {
					doc.insertString(doc.getLength(), sub ,
							attr);
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		}
		try {
			doc.insertString(doc.getLength(), "\n" ,
					attr);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
}
最近下载更多
cksndh  LV4 2023年8月16日
fuyouou  LV5 2023年6月29日
yuanchuang  LV22 2023年2月14日
liys1234  LV9 2022年6月21日
微信网友_6003487859068928  LV5 2022年6月15日
微信网友_5989987974549504  LV5 2022年6月5日
17岁的孩子想糖吃  LV7 2021年12月31日
111111255  LV2 2021年12月24日
Aoifee  LV4 2021年11月10日
pear_li  LV1 2021年10月13日
最近浏览更多
krispeng  LV6 4月15日
陈小灏  LV14 2023年12月26日
luamweise 2023年10月18日
暂无贡献等级
wertttak  LV1 2023年9月26日
小新Coding  LV9 2023年9月7日
cksndh  LV4 2023年8月16日
fuyouou  LV5 2023年6月29日
2017143155  LV12 2023年6月24日
漫步的海星  LV4 2023年5月9日
微信网友_6444139264921600  LV6 2023年4月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友