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

📄 p2pconvsform.java

📁 一个仿qq的程序源码 一个用纯java开发的
💻 JAVA
字号:
package qianqian.p2pchat.gui;

import java.awt.MediaTracker;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.text.*;

import qianqian.p2pchat.constant.Const;
import qianqian.p2pchat.control.*;
import qianqian.p2pchat.filetrans.RecvfileThread;
import qianqian.p2pchat.io.SafePrintWriter;
import qianqian.p2pchat.tools.Tools;

public class P2PConvsForm extends javax.swing.JFrame {
	private static final long serialVersionUID = 1L;
	private static final SimpleAttributeSet[] attrs = initAttributes();
	public static int SYS_STYLE = Const.SYS_FONT;
	public static int SET_STYLE = Const.SET_FONT;
	private P2PConversation p2pConvs;
	private javax.swing.JScrollPane sendScroller;
	private javax.swing.JScrollPane recvScroller;
	private javax.swing.JTextPane msgArea;
	private javax.swing.JTextArea sendArea;
	private javax.swing.JSplitPane splitPane;
	private javax.swing.text.AbstractDocument doc;
	private javax.swing.border.MatteBorder border;
	private javax.swing.JFileChooser fileChooser;

	public P2PConvsForm(P2PConversation convs) {
		p2pConvs = convs;
		initComponents();
		setTitle(p2pConvs.getTo().getName());
		if (p2pConvs.getTo().getGender() == '男') {
			setIconImage(new javax.swing.ImageIcon("icons/QG.gif").getImage());
		} else {
			setIconImage(new javax.swing.ImageIcon("icons/QM.gif").getImage());
		}
		setBounds(357, 150, 310, 350);
		requestFocus();
		sendArea.requestFocus();
	}

	private void initComponents() {
		setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
		sendArea = new javax.swing.JTextArea();
		sendScroller = new javax.swing.JScrollPane();
		msgArea = new javax.swing.JTextPane();
		recvScroller = new javax.swing.JScrollPane();
		splitPane = new javax.swing.JSplitPane(
				javax.swing.JSplitPane.VERTICAL_SPLIT);
		border = new javax.swing.border.MatteBorder(new java.awt.Insets(2, 2,
				2, 2), new java.awt.Color(195, 225, 255));
		fileChooser = new javax.swing.JFileChooser();
		fileChooser.setDialogType(javax.swing.JFileChooser.FILES_ONLY);

		sendArea.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyTyped(java.awt.event.KeyEvent evt) {
				typeBoxKeyTyped(evt);
			}
		});

		sendArea.setLineWrap(true);
		sendArea.setBorder(border);
		sendScroller.setViewportView(sendArea);

		msgArea.setBorder(border);
		msgArea.setEditable(false);
		StyledDocument styledDoc = msgArea.getStyledDocument();
		if (styledDoc instanceof AbstractDocument) {
			doc = (AbstractDocument) styledDoc;
		} else {
		}
		recvScroller.setViewportView(msgArea);

		splitPane.setBottomComponent(sendScroller);
		splitPane.setTopComponent(recvScroller);
		splitPane.setDividerLocation(220);
		splitPane.setDividerSize(2);

		getContentPane().add(splitPane);
		pack();
	}

	private void typeBoxKeyTyped(java.awt.event.KeyEvent evt) {
		if (evt.getKeyChar() == '\n')
			sendMsg();
	}

	public void sendMsg() {
		String msg = sendArea.getText().trim();
		sendArea.setText("");
		if (msg.isEmpty()) {
			addRecvLine("发送内容为空!\n\n", SYS_STYLE);
			return;
		}
		if (msg.startsWith("/") && callCommand(msg)) {
			return;
		}
		p2pConvs.sendMsg(Const.CODE_TEXT_MSG, msg);
	}

	private boolean callCommand(String msg) {
		boolean isCalled = true;
		if (msg.equals("/cls")) {// 清除聊天屏幕文字
			msgArea.setText("");
		} else if (msg.equals("/help")) {// 显示命令行参数
			showHelp();
		} else if (msg.matches("/font-[0-9]$")) {// 设置聊天屏幕显示字体
			ChangeStyle(msg.charAt(msg.indexOf('-') + 1) - 48);
		} else if (msg.equals("/quit")) {// 关闭窗口
			setVisible(false);
		} else if (msg.equals("/time")) {// 显示系统时间
			addRecvLine("当前时间:\t" + Tools.getTimeInfo() + "\n\n", SYS_STYLE);
		} else if (msg.matches("/mask-[0-1]$")) {// 屏蔽|取消屏蔽对方消息
			maskPeer(msg.charAt(msg.indexOf('-') + 1));
		} else if (msg.equals("/geti")) {// 获取对方基本信息
			showPeer();
		} else if (msg.equals("/sendf")) {// 发送文件
			sendFile();
		} else if (msg.equals("/recvf")) {// 接收文件
			recvFile();
		} else if (msg.equals("/cans")) {// 取消发送文件
			cancelSend();
		} else if (msg.equals("/canr")) {// 取消接收文件
			cancelRecv();
		} else if (msg.equals("/image")) {// 发送图片消息
			sendImage();
		} else if (msg.equals("/face")) {// 获得表情
			getFace();
		} else if (msg.startsWith("/face-")) {// 发送表情
			p2pConvs.sendMsg(Const.CODE_FACE_MSG, msg);
		} else {
			isCalled = false;
		}
		return isCalled;
	}

	public void addRecvLine(String iMsg, int style) {
		try {
			doc.insertString(doc.getLength(), iMsg, attrs[style]);
		} catch (Exception e) {
		}
		msgArea.setCaretPosition(doc.getLength());
		sendArea.requestFocus();
	}

	public void addRecvImage(String info, String image) {
		try {
			doc.insertString(doc.getLength(), info + "\n", attrs[SYS_STYLE]);
		} catch (Exception e) {
		}
		msgArea.setCaretPosition(doc.getLength());
		msgArea.insertIcon(new javax.swing.ImageIcon(image));
		try {
			doc.insertString(doc.getLength(), "\n\n", attrs[SET_STYLE]);
		} catch (Exception e) {
		}
		msgArea.setCaretPosition(doc.getLength());
		sendArea.requestFocus();
	}

	public void setVisible(boolean isVisible) {
		if (!isVisible) {
			if (p2pConvs.isSending())
				isVisible = !cancelSend();
			if (p2pConvs.isRecving())
				isVisible = !cancelRecv();
			msgArea.setText("");
			sendArea.setText("");
		}
		super.setVisible(isVisible);
	}

	private void ChangeStyle(int style) {
		SET_STYLE = style % 10;
		addRecvLine("字体设置成功!\n\n", SYS_STYLE);
	}

	private void showHelp() {
		try {
			doc
					.insertString(doc.getLength(), Const.HELP_TEXT,
							attrs[SYS_STYLE]);
		} catch (Exception e) {
		}
		msgArea.setCaretPosition(doc.getLength());
	}

	private void maskPeer(char code) {
		if (code == '1') {
			p2pConvs.setShutUp(true);
			addRecvLine("成功屏蔽该用户!\n\n", SYS_STYLE);
		} else {
			p2pConvs.setShutUp(false);
			addRecvLine("解除屏蔽该用户!\n\n", SYS_STYLE);
		}
	}

	private void showPeer() {
		String info = "好友信息:\t昵称:" + p2pConvs.getTo().getName() + "\t性别:"
				+ p2pConvs.getTo().getGender() + "\n\n";
		addRecvLine(info, SYS_STYLE);
	}

	private void sendFile() {
		if (p2pConvs.isSending()) {
			addRecvLine("上次发送未完成,/cans可取消发送!\n\n", SYS_STYLE);
			return;
		}
		if (fileChooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
			p2pConvs.setSending(true);
			p2pConvs.setSendFile(fileChooser.getSelectedFile());
			try {
				p2pConvs.sendMsg(Const.CODE_FILE_SEND, p2pConvs.getSendFile()
						.getCanonicalPath());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			addRecvLine("没有选定发送文件,命令取消!\n\n", SYS_STYLE);
		}
	}

	private void recvFile() {
		if (!p2pConvs.isRecving()) {
			addRecvLine("对方没有给您发送文件,命令取消!\n\n", SYS_STYLE);
			return;
		}
		if (p2pConvs.getSocket() != null) {
			addRecvLine("上次接收未完成,/canr可取消接收!\n\n", SYS_STYLE);
			return;
		}

		p2pConvs
				.sendMsg(Const.CODE_FILE_RECV, p2pConvs.getRecvFile().getName());
		try {
			java.net.Socket socket = new java.net.Socket(p2pConvs.getToAddr(),
					Const.FilePort);
			p2pConvs.setSocket(socket);
			SafePrintWriter out = new SafePrintWriter(
					new java.io.OutputStreamWriter(socket.getOutputStream()),
					true, "\n");
			java.io.InputStream in = socket.getInputStream();

			File saveFile = null;
			fileChooser.setSelectedFile(new File(p2pConvs.getRecvFile()
					.getName()));
			if (fileChooser.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
				saveFile = fileChooser.getSelectedFile();
			} else {
				saveFile = new File("download" + File.separator
						+ p2pConvs.getRecvFile().getName());
			}

			out.println(p2pConvs.getRecvFile().getCanonicalPath());
			p2pConvs.setRecvThread(new RecvfileThread(in, out, saveFile,
					p2pConvs));
			p2pConvs.startRecv();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private boolean cancelSend() {
		boolean cancel = false;
		if (p2pConvs.isSending()) {
			int msg = javax.swing.JOptionPane.showConfirmDialog(this,
					"是否取消发送文件?", "提示", javax.swing.JOptionPane.YES_NO_OPTION);
			if (msg == 0) {
				p2pConvs.sendMsg(Const.CODE_FILE_CANS, p2pConvs.getSendFile()
						.getName());
				p2pConvs.resetFileSender();
				cancel = true;
			}
		} else {
			addRecvLine("当前没有发送文件,命令取消!\n\n", SYS_STYLE);
		}
		return cancel;
	}

	private void sendImage() {
		if (fileChooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
			File img = fileChooser.getSelectedFile();
			String fileType = img.getName().substring(
					img.getName().lastIndexOf('.')).toLowerCase();
			if (Const.IMAGE_TYPE.indexOf(fileType) == -1) {
				addRecvLine("只支持gif/jpg/png格式的图片,命令取消!\n\n", SYS_STYLE);
				return;
			}
			if (img.length() > Const.RecvBufSize) {
				addRecvLine("只支持大小为64K以下的图片,命令取消!\n\n", SYS_STYLE);
				return;
			}
			try {
				p2pConvs.sendMsg(Const.CODE_IMAGE_MSG, img.getCanonicalPath());
			} catch (Exception e) {
			}
		}
	}

	public void addRecvFace(String info, String face) {
		try {
			doc.insertString(doc.getLength(), info + "\n", attrs[SYS_STYLE]);
			msgArea.setCaretPosition(doc.getLength());

			String[] faces = face.split("\\.");
			ImageIcon icon = null;
			for (String i : faces) {
				icon = new ImageIcon("face/" + i.trim() + ".gif");
				if (icon.getImageLoadStatus() == MediaTracker.COMPLETE) {
					msgArea.insertIcon(icon);
				} else {
					doc.insertString(doc.getLength(), i, attrs[SET_STYLE]);
					msgArea.setCaretPosition(doc.getLength());
				}
			}
			doc.insertString(doc.getLength(), "\n\n", attrs[SET_STYLE]);
		} catch (Exception e) {
		}
	}

	private void getFace() {
		new Thread(new Runnable() {
			public void run() {
				FaceForm.getInstance(P2PConvsForm.this, sendArea).setVisible(
						true);
			}
		}).start();
	}

	private boolean cancelRecv() {
		boolean cancel = false;
		if (p2pConvs.isRecving()) {
			int msg = javax.swing.JOptionPane.showConfirmDialog(this,
					"是否取消接收文件?", "提示", javax.swing.JOptionPane.YES_NO_OPTION);
			if (msg == 0) {
				p2pConvs.sendMsg(Const.CODE_FILE_CANR, p2pConvs.getRecvFile()
						.getName());
				p2pConvs.resetFileRecver();
				cancel = true;
			}
		} else {
			addRecvLine("当前没有接收文件,命令取消!\n\n", SYS_STYLE);
		}
		return cancel;
	}

	// 初始化字体风格
	protected static SimpleAttributeSet[] initAttributes() {
		SimpleAttributeSet[] attrs = new SimpleAttributeSet[10];

		attrs[0] = new SimpleAttributeSet();
		StyleConstants.setFontFamily(attrs[0], "SansSerif");
		StyleConstants.setFontSize(attrs[0], 12);

		attrs[1] = new SimpleAttributeSet(attrs[0]);
		StyleConstants.setForeground(attrs[1], new java.awt.Color(5, 105, 205));
		StyleConstants.setBold(attrs[1], true);

		attrs[2] = new SimpleAttributeSet(attrs[0]);
		StyleConstants
				.setForeground(attrs[2], new java.awt.Color(15, 115, 225));
		StyleConstants.setItalic(attrs[2], true);

		attrs[3] = new SimpleAttributeSet(attrs[0]);
		StyleConstants
				.setForeground(attrs[3], new java.awt.Color(25, 125, 235));
		StyleConstants.setFontSize(attrs[3], 14);

		attrs[4] = new SimpleAttributeSet(attrs[0]);
		StyleConstants
				.setForeground(attrs[4], new java.awt.Color(35, 135, 245));
		StyleConstants.setFontSize(attrs[4], 16);

		attrs[5] = new SimpleAttributeSet(attrs[0]);
		StyleConstants
				.setForeground(attrs[5], new java.awt.Color(45, 145, 255));
		StyleConstants.setBold(attrs[5], true);

		attrs[6] = new SimpleAttributeSet(attrs[0]);
		StyleConstants
				.setForeground(attrs[6], new java.awt.Color(255, 136, 83));
		StyleConstants.setBold(attrs[6], true);

		attrs[7] = new SimpleAttributeSet(attrs[0]);
		StyleConstants
				.setForeground(attrs[7], new java.awt.Color(83, 136, 108));
		StyleConstants.setBold(attrs[7], true);

		attrs[8] = new SimpleAttributeSet(attrs[0]);
		StyleConstants
				.setForeground(attrs[8], new java.awt.Color(136, 83, 108));
		StyleConstants.setItalic(attrs[8], true);

		attrs[9] = new SimpleAttributeSet(attrs[0]);
		StyleConstants
				.setForeground(attrs[9], new java.awt.Color(255, 136, 83));
		StyleConstants.setFontSize(attrs[9], 14);

		return attrs;
	}
}

⌨️ 快捷键说明

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