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

📄 daswdasd.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.*;
import java.net.*;
import javax.swing.text.*;

public class daswdasd extends JFrame implements ActionListener, KeyListener, WindowListener
{
	JLabel lAim;
	JLabel lHead;
	JTextPane tContent;
	JTextArea tInput;
	JButton bSend;
	JButton bClose;
	JScrollPane sContent;
	JScrollPane sInput;
	JScrollBar sb;
	JPanel pTop;
	JPanel pBottom;	
	JLabel lColor;
	JLabel lFontSize;
	JComboBox cbColor;
	JComboBox cbFontSize;
	JCheckBox chbB;
	
	Color col = Color.BLACK;
	boolean blod = false;
	int fontSize = 12;
	
	cawesda m;
	cawesda me;
	File f ;
	Socket s;
	int frames;
	public void setL(int n)
	{
		frames = n;
	}
	public void setSocket(Socket s)
	{
		this.s = s;
	}
	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;
		}
	}
	daswdasd(cawesda me, cawesda m)
	{
		this.me = me;
		this.m = m;
		this.setTitle("与" + m.getName() +"聊天中");
		this.setBounds(300 + frames * 10, 100 + frames * 10,450,450);
		this.setResizable(false);
		this.setLayout(null);
		this.addWindowListener(this);
		
		lHead = new JLabel("");
		lHead.setBounds(20,0,50,50);
		lHead.setIcon(new ImageIcon(getImageSource("face/" + m.getHeadNum() + ".gif")));
		
		lAim = new JLabel(m.getName());
		lAim.setBounds(75,0,200,50);
		
		
		tContent = new JTextPane();
		tContent.setBackground(Color.WHITE);
		tContent.setFont(new Font("宋体", Font.PLAIN, 12));
		tContent.setEditable(false);
		
		sContent = new JScrollPane(tContent);
		sContent.setBounds(20,50,400,200);
		
		pTop = new JPanel();
		pTop.setBackground(new Color(231, 243, 255));
		pTop.setBounds(0,0,450,250);
		pTop.setLayout(null);
		pTop.add(lHead);
		pTop.add(lAim);
		pTop.add(sContent);
		
		tInput = new JTextArea(10,10);
		tInput.setRows(3);
		tInput.grabFocus();
		tInput.addKeyListener(this);
		
		sInput = new JScrollPane(tInput);
		sInput.setBounds(20,40,400,80);
		
		lColor = new JLabel("颜色");
		lColor.setFont(new Font("宋体", Font.PLAIN, 12));
		lColor.setBounds(20,15,40,20);
		
		cbColor = new JComboBox();
		cbColor.setBounds(50,15,50,20);
		cbColor.setFont(new Font("宋体", Font.PLAIN, 12));
		cbColor.setBackground(Color.WHITE);
		cbColor.addItem("<html><body bgcolor = black>&nbsp;&nbsp;&nbsp;&nbsp;</body></html>");
		cbColor.addItem("<html><body bgcolor = red>&nbsp;&nbsp;&nbsp;&nbsp;</body></html>");
		cbColor.addItem("<html><body bgcolor = blue>&nbsp;&nbsp;&nbsp;&nbsp;</body></html>");
		cbColor.addItem("<html><body bgcolor = yellow>&nbsp;&nbsp;&nbsp;&nbsp;</body></html>");
		cbColor.addItem("<html><body bgcolor = green>&nbsp;&nbsp;&nbsp;&nbsp;</body></html>");
		cbColor.addActionListener(this);
		
		lFontSize = new JLabel("大小");
		lFontSize.setFont(new Font("", Font.PLAIN, 12));
		lFontSize.setBounds(110,15,40,20);
		
		cbFontSize = new JComboBox();
		cbFontSize.setBounds(140,15,40,20);
		cbFontSize.setFont(new Font("宋体", Font.PLAIN, 12));
		cbFontSize.setBackground(Color.WHITE);
		for(int i = 8; i <= 30; i+=2)
		{
			cbFontSize.addItem(Integer.toString(i));
		}
		cbFontSize.setSelectedIndex(2);
		cbFontSize.addActionListener(this);
		
		chbB = new JCheckBox("<html><h2>B</h2></html>");
		chbB.setBackground(new Color(231, 243, 255));
		chbB.setBounds(190,15,40,20);
		chbB.addActionListener(this);
		
		bSend = new JButton("发送");
		bSend.setFont(new Font("宋体", Font.PLAIN, 12));
		bSend.setBounds(360,140,60,20);
		bSend.addActionListener(this);
		
		bClose = new JButton("关闭");
		bClose.setFont(new Font("宋体", Font.PLAIN, 12));
		bClose.setBounds(290,140,60,20);
		bClose.addActionListener(this);
		
		pBottom = new JPanel();
		pBottom.setBounds(0,250,450,190);
		pBottom.setFont(new Font("宋体", Font.PLAIN, 12));
		pBottom.setBackground(new Color(231, 243, 255));
		pBottom.setLayout(null);
		pBottom.add(lColor);
		pBottom.add(cbColor);
		pBottom.add(lFontSize);
		pBottom.add(cbFontSize);
		pBottom.add(chbB);
		pBottom.add(sInput);
		pBottom.add(bSend);
		pBottom.add(bClose);
		
		this.add(pTop);
		this.add(pBottom);
		f = new File("log/" + m.getID() + "log.qq");
		try
		{
			String tmp;
			FileReader fr = new FileReader(f);
			BufferedReader br = new BufferedReader(fr);
			int lines = 0;
			while((tmp = br.readLine()) != null)
			{
				lines++;
			}
			br.close();
			fr = new FileReader(f);
			br = new BufferedReader(fr);
			int p = 0;
			while(p < lines - 12)
			{
				br.readLine();
				p++;
			}
			while((tmp = br.readLine()) != null)
			{
				setDocs(tmp, Color.LIGHT_GRAY, false, 12);
			}
			br.close();
		}
		catch(Exception e)
		{
			try 
			{
				FileWriter fr = new FileWriter(f);
				PrintWriter pw = new PrintWriter(fr);
				pw.close();
			}
			catch (IOException e1) 
			{
				e1.printStackTrace();
			}
		}
		tContent.setText(tContent.getText() + "\n");
	}
	public void insert(String str, AttributeSet attrSet)
	{
		Document doc = tContent.getDocument();
		str = str + "\n";
		try
		{
			doc.insertString(doc.getLength(), str, attrSet);
		}
		catch(Exception e)
		{
			
		}
	}
	public void setDocs(String str, Color col, boolean blod, int fontSize)
	{
		SimpleAttributeSet attrSet = new SimpleAttributeSet();
		StyleConstants.setForeground(attrSet, col);
		if(blod)
		{
			StyleConstants.setBold(attrSet, true);
		}
		StyleConstants.setFontSize(attrSet, fontSize);
		StyleConstants.setFontFamily(attrSet, "宋体");
		insert(str, attrSet);
		sContent.getVerticalScrollBar().setValue(sContent.getVerticalScrollBar().getMaximum());
	}
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == bSend)
		{
			if(tInput.getText().equals(""))
			{
				tInput.setText("输入不能为空。");
			}
			else
			{
				String chat = tInput.getText();
				setDocs(me.getName() + "说:", Color.BLACK, false, 12);
				setDocs(chat, col, blod, fontSize);
			
				setChatToServer(chat, col, blod, fontSize);
				
				chat = me.getName() + "说:\n" + chat;
				setChatToFile(chat);
				
				tInput.setText("");
				tInput.grabFocus();
			}
		}
		if(e.getSource() == bClose)
		{
			this.dispose();
		}
		if(e.getSource() == cbColor)
		{			
			Color[] fColor = 
			{
				Color.BLACK,
				Color.RED,
				Color.BLUE,
				Color.YELLOW,
				Color.GREEN
			};
			for(int i = 0; i <= 4; i++)
			{
				if(cbColor.getSelectedIndex() == i )
				{
					tInput.setForeground(fColor[i]);
					col = fColor[i];
				}
			}
		}
		if(e.getSource() == cbFontSize)
		{
			for(int i = 0; i <= 11; i++)
			{
				if(cbFontSize.getSelectedIndex() == i)
				{
					System.out.println(cbFontSize.getSelectedIndex());
					fontSize = (i+4)*2;
					tInput.setFont(new Font("", Font.PLAIN, fontSize));
					
				}
			}
		}
		if(e.getSource() == chbB)
		{
			if(chbB.isSelected())
			{
				tInput.setFont(new Font("宋体", Font.BOLD, fontSize));
				blod = true;
			}
			else
			{
				tInput.setFont(new Font("宋体", Font.PLAIN, fontSize));
				blod = false;
			}
		}
	}

	public void setChatToFile(String str)
	{
		try 
		{
			FileWriter fr = new FileWriter(f,true);
			PrintWriter pw = new PrintWriter(fr,true);
			pw.println(str);
		}
		catch(Exception e)
		{
			
		}
	}

	public void setChatToServer(String str, Color col, boolean b, int size)
	{
		Zdasdasd ms = new Zdasdasd(s);
		String cs = null;
		if(col == Color.GREEN)
		{
			cs = "GREEN";
		}
		else if(col == Color.RED)
		{
			cs = "RED";
		}
		else if(col == Color.BLUE)
		{
			cs = "BLUE";
		}
		else if(col == Color.YELLOW)
		{
			cs = "YELLOW";
		}
		else
		{
			cs = "BLACK";
		}
		ms.println("CHAT==com==" + me.getID() + "==QQ==" + m.getID() + "==QQ==" + str + "==STYLE==" + cs + "==STYLE==" + b + "==STYLE==" + size);
	}
	public void dealChat(String allChatAndStyle)
	{
		if(allChatAndStyle.indexOf("==ELSE==") == -1)
		{
			String sep = "==STYLE==";
			String chat = allChatAndStyle.split(sep)[0];
			String cs = allChatAndStyle.split(sep)[1];
			String b = allChatAndStyle.split(sep)[2];
			int size = Integer.parseInt(allChatAndStyle.split(sep)[3]);
			
			boolean isB = b.equals("true") ? true : false;
			Color c = null;
			if(cs.equals("BLUE"))
			{
				c = Color.BLUE;
			}
			else if(cs.equals("RED"))
			{
				c = Color.RED;
			}
			else if(cs.equals("GREEN"))
			{
				c = Color.GREEN;
			}
			else if(cs.equals("YELLOW"))
			{
				c = Color.YELLOW;
			}
			else
			{
				c = Color.BLACK;
			}
			setDocs(m.getName() + "说:", Color.BLACK, false, 12);
			setDocs(chat, c, isB, size);
		}

		String chat = allChatAndStyle.split("==STYLE==")[0];
		chat = m.getName() + "说:\n" + chat;
		setChatToFile(chat);
	}
	public void setOtherSideLeave()
	{
		setDocs("服务器:\n",Color.RED,false,12);
		setDocs("对方已经下线。",Color.RED,false,12);
	}
	public void keyTyped(KeyEvent e) 
	{
		
	}
	public void keyPressed(KeyEvent e) 
	{
		//System.out.println(e.getKeyCode());
	}
	public void keyReleased(KeyEvent e) {
		
	}
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void windowClosing(WindowEvent e) 
	{
		this.dispose();
		
	}
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	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
		
	}

}

⌨️ 快捷键说明

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