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

📄 liaotian.java

📁 JAVAQQ 聊天系统,信息发送.改变头像.等功能,时时聊天.
💻 JAVA
字号:
package com.li.myqq.login;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;

import javax.swing.JScrollPane;
import java.awt.Rectangle;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JComboBox;



public class LiaoTian extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JScrollPane jScrollPane = null;

	private JTextArea recieve = null;

	private JButton zhuti = null;

	private JPanel jPanel = null;

	private JScrollPane jScrollPane1 = null;

	private JTextArea send = null;

	private JButton JBsend = null;

	private JButton JBcancel = null;

	private JButton jButton = null;

	private JButton jButton1 = null;

	private JComboBox jComboBox = null;

	private JButton jilu = null;

	private JButton jButton11 = null;
	BufferedReader bur=null;
	PrintWriter pw=null;
	Socket socket=null;  //  @jve:decl-index=0:
	/**
	 * This is the default constructor
	 */
	public LiaoTian() {
		super();
		
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		socket=getSocket();
		this.setSize(488, 358);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage("./pic/84.gif"));
		this.setContentPane(getJContentPane());
		this.setTitle("聊天程序");
		jContentPane.setBackground(Color.pink);
		jComboBox.addItem("你好啊!交个朋友吧!");
		jComboBox.addItem("正在工作当中,请务打扰!");
		jComboBox.addItem("祝你开心快乐每一天!!!");
		jComboBox.addItem("亲爱的 晚安!!!");
		jComboBox.addItem("我还有事,先走了!下次再聊,拜拜!");
		String date=new SimpleDateFormat("yyyy-MM-dd   hh:mm:ss").format(new Date()).toString();
		System.out.println(date);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getZhuti(), null);
			jContentPane.add(getJPanel(), null);
			jContentPane.add(getJScrollPane1(), null);
			jContentPane.add(getJBsend(), null);
			jContentPane.add(getJBcancel(), null);
			jContentPane.add(getJilu(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(-1, 26, 483, 132));
			jScrollPane.setViewportView(getRecieve());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes recieve	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	public JTextArea getRecieve() {
		if (recieve == null) {
			recieve = new JTextArea();
		}
		return recieve;
	}

	/**
	 * This method initializes zhuti	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getZhuti() {
		if (zhuti == null) {
			zhuti = new JButton();
			zhuti.setBounds(new Rectangle(0, -1, 484, 27));
			zhuti.setBackground(new Color(255, 153, 153));
		}
		return zhuti;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(0, 156, 482, 49));
			jPanel.setBackground(Color.pink);
			jPanel.setFont(new Font("\u534e\u6587\u884c\u6977", Font.BOLD | Font.ITALIC, 36));
			jPanel.add(getJButton(), null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJComboBox(), null);
			jPanel.add(getJButton11(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(new Rectangle(-1, 204, 482, 88));
			jScrollPane1.setViewportView(getSend());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes send	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	public JTextArea getSend() {
		if (send == null) {
			send = new JTextArea();
		}
		return send;
	}

	/**
	 * This method initializes JBsend	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJBsend() {
		if (JBsend == null) {
			JBsend = new JButton();
			JBsend.setBounds(new Rectangle(202, 298, 78, 28));
			JBsend.setBackground(new Color(255, 153, 153));
			JBsend.setText("发送");
			JBsend.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(getSend().getText().trim().length()>0)
					{
						String str=getSend().getText().trim();
						getSendMessage(str);
						getRecieve().setText("\n"+getRecieve().getText()+"\n"+"服务端:"+"\t"+str);
						getSend().setText("");
					}
					else
					{
						JOptionPane.showMessageDialog(null, "不能发送空信息");
						
					}
					
				}
			});
		}
		return JBsend;
	}

	/**
	 * This method initializes JBcancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJBcancel() {
		if (JBcancel == null) {
			JBcancel = new JButton();
			JBcancel.setBounds(new Rectangle(368, 297, 82, 29));
			JBcancel.setBackground(new Color(255, 153, 153));
			JBcancel.setText("关闭");
			JBcancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return JBcancel;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(9, 7, 38, 33));
			jButton.setBackground(new Color(255, 153, 153));
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(58, 8, 65, 31));
			jButton1.setText("A");
			jButton1.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 24));
			jButton1.setBackground(new Color(255, 153, 153));
		}
		return jButton1;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setBounds(new Rectangle(319, 8, 154, 31));
			jComboBox.setBackground(Color.pink);
		}
		return jComboBox;
	}

	/**
	 * This method initializes jilu	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJilu() {
		if (jilu == null) {
			jilu = new JButton();
			jilu.setBounds(new Rectangle(31, 297, 90, 29));
			jilu.setBackground(new Color(255, 153, 153));
			jilu.setText("聊天记录");
		}
		return jilu;
	}

	/**
	 * This method initializes jButton11	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton11() {
		if (jButton11 == null) {
			jButton11 = new JButton();
			jButton11.setBounds(new Rectangle(144, 9, 83, 30));
			jButton11.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 18));
			jButton11.setText("Color");
			jButton11.setBackground(new Color(255, 153, 153));
		}
		return jButton11;
	}
	  public void getSendMessage(String str){
		  String st=str;
     	 PrintWriter pw;
			try {
				pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
	        	 pw.println(st);
	        	 pw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
      }

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}
} 

class ClientThread extends Thread
{
	Socket socket;
	LiaoTian lt;
	  public void run()
      {
		  BufferedReader bf;
		      while(true)
 		       {
		    	  try {	
		        	  if(socket!=null && socket.getInputStream().available()>0)
		        	  {
						 bf=new BufferedReader(new InputStreamReader(socket.getInputStream()));
						 lt.getSend().setText(lt.getSend().getTabSize()+"\n"+"服务端:"+"\t"+bf.readLine());
						 bf.close(); 
		        	  }
		        	  try {
						sleep(500);
					} catch (InterruptedException e) {
						// TODO 自动生成 catch 块
						e.printStackTrace();
					}
					   } catch (IOException e) {
						// TODO 自动生成 catch 块
						e.printStackTrace();
					   }
 		       }
     	 }
	
	}

⌨️ 快捷键说明

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