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

📄 chatthread.java

📁 类似qq的以c/s结构和p2p结构结合的聊天软件。
💻 JAVA
字号:
package com.client;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.ServerSocket;
import java.awt.Font;
import java.awt.Color;
import java.awt.FileDialog;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JScrollBar;
import javax.swing.JColorChooser;
import javax.swing.text.*;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import java.awt.Container;
import java.awt.BorderLayout;
import java.util.Date;
import java.lang.String.*;
class ChatThread extends JFrame implements  Runnable
{
	public static JTextArea send;
	private static JTextPane rcvPane;
	private static InputStreamReader is;
	private static PrintWriter os;
	private static String readline;
	private static BufferedReader buffer;
	private static Socket chatSocket;
	private static ServerSocket fileServerSocket;
	private static Socket fileClientSocket;
	private static String flag;
	private static String destIP;
	private static String otherSide;
	private static String clientName;
	private static Date date;
	private static int port;
	private static SimpleAttributeSet sendAttrSet;
	private static SimpleAttributeSet rcvAttrSet;
	private static Document doc;
	public static int fontSize;
	public static String fontFamily;
	public static Color fontColor;
	public static Boolean fontBold;
	private static String message;
	private static String[] rcvContent;
	public ChatThread(String flag,String destIP,String otherSide,int port) 
	{
		super();
		setIconImage(new ImageIcon(this.getClass().getResource("image/bird.jpg")).getImage());
		this.flag=flag;
		this.destIP=destIP;
		this.port=port;
		this.otherSide=otherSide;
		rcvContent=new String[7];
		fontSize=16;
		fontFamily="楷体";
		fontColor=Color.BLUE;
		fontBold=false;
		date=new Date();
		message="";
		sendAttrSet=new SimpleAttributeSet();
		rcvAttrSet=new SimpleAttributeSet();
		if(flag=="server")
		{
			try
			{
				ServerSocket serverSocket=new ServerSocket(port);
				this.chatSocket=serverSocket.accept();
			}catch(Exception de){}		
		}
		if(flag=="client")
		{
			try
			{
			    this.chatSocket = new Socket(destIP,port);
			    setTitle("与"+ChatThread.otherSide+"对话中");
			}catch(Exception e)
			{
				MessageDialog dialog=new MessageDialog("无法连接!");
   	            dialog.setSize(331, 163);
   	            return;
			}
		}
		getContentPane().setLayout(null);
		setBounds(100, 100, 386, 378);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		rcvPane = new JTextPane();
		rcvPane.setBounds(10, 10, 357, 195);
		final JScrollPane scrollPane= new JScrollPane();
		scrollPane.setBounds(10, 34, 357, 153);
		getContentPane().add(scrollPane);
		scrollPane.setViewportView(rcvPane);
		doc=rcvPane.getDocument();

		final JButton button_send = new JButton();
		button_send.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				try
				{
					StyleConstants.setFontSize(sendAttrSet, fontSize);
					StyleConstants.setBold(sendAttrSet, fontBold);
					StyleConstants.setForeground(sendAttrSet,fontColor);
					StyleConstants.setFontFamily(sendAttrSet, fontFamily);
                    message=send.getText()+'#'+fontSize+'#'+fontBold+'#'+fontColor.getRed()+'#'+fontColor.getGreen()+'#'+fontColor.getBlue()+'#'+fontFamily;
					doc.insertString(doc.getLength(),date.getHours()+"时:"+date.getMinutes()+"分  "+"我说:"+'\n'+send.getText()+'\n',sendAttrSet);
					os.println(message);
					send.setText("");
					os.flush();
				}catch(Exception e){}
				
			}
		});
		button_send.setText("发送");
		button_send.setBounds(234, 313, 99, 23);
		getContentPane().add(button_send);

		send = new JTextArea();
		send.setBounds(10, 240, 357, 67);
		getContentPane().add(send);

		final JButton button_close = new JButton();
		button_close.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				dispose();
			}
		});
		button_close.setText("关闭");
		button_close.setBounds(59, 313, 99, 23);
		getContentPane().add(button_close);

		final JButton file = new JButton();
		file.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				 /*try
				 {
					 Socket requestSocket=new Socket(ChatThread.destIP,8890);
					 ObjectOutputStream out=new ObjectOutputStream(requestSocket.getOutputStream());
					 ObjectInputStream in=new ObjectInputStream(requestSocket.getInputStream());
					 out.writeObject("file");
					 out.flush();
					 int po=(Integer)in.readObject();
					 System.out.println(po+"1");
					 FileSend fsend=new FileSend(ChatThread.destIP,po);
				     Thread thread=new Thread(fsend);
					 fsend.setVisible(true);
					 thread.start();
				 }catch(Exception ee){}*/
			}
		});
		file.setText("传送文件");
		file.setBounds(253, 211, 93, 23);
		getContentPane().add(file);

		final JButton fontStyle = new JButton();
		fontStyle.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0)
			{
				FontDialog fDialog=new FontDialog();
				fDialog.setVisible(true);
			}
		});
		fontStyle.setText("字体样式");
		fontStyle.setBounds(10, 211, 87, 23);
		getContentPane().add(fontStyle);

		final JButton color = new JButton();
		color.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				JColorChooser colorchooser=new JColorChooser();
				colorchooser.setVisible(true);
				Color selectedColor=colorchooser.showDialog(color, "颜色选择",Color.BLACK);
                fontColor=selectedColor;
				send.setForeground(selectedColor);
                send.repaint();
			}
		});
		color.setText("字体颜色");
		color.setBounds(127, 211, 87, 23);
		getContentPane().add(color);
	}
	public void run()
	{
		try
        { 			
         try{
        	   os = new PrintWriter(chatSocket.getOutputStream());
        	   is = new InputStreamReader(chatSocket.getInputStream());
        	   buffer= new BufferedReader(is);
             }catch(IOException ioe){}
             
             if(flag=="client")
             {
            	 os.println(MainFrame.clientName);
            	 os.flush();
             }
             if(flag=="server")
             {
            	 
            	  otherSide=buffer.readLine();
            	  setTitle("与"+otherSide+"对话中");
            	 repaint();
             }
        	 
      	 readline=buffer.readLine();
      	 while(true)
      	 {
      	    rcvContent=readline.split("#");
      		StyleConstants.setFontSize(rcvAttrSet, Integer.parseInt(rcvContent[1]));
			StyleConstants.setBold(rcvAttrSet, Boolean.parseBoolean(rcvContent[2]));
			StyleConstants.setForeground(rcvAttrSet,new Color(Integer.parseInt(rcvContent[3]),Integer.parseInt(rcvContent[4]),Integer.parseInt(rcvContent[5])));
			StyleConstants.setFontFamily(rcvAttrSet, rcvContent[6]);
            doc.insertString(doc.getLength(),date.getHours()+"时:"+date.getMinutes()+"分  "+ChatThread.otherSide+" 说:"+'\n'+rcvContent[0]+'\n',rcvAttrSet);
      		 readline = buffer.readLine();
      	 }
        }catch(Exception e){}      
	}
}

⌨️ 快捷键说明

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