先编辑页面

package com.lovo.feichun;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

public class FeiChunFrame extends JFrame{

private Container contentP;

private JTextArea msgArea;//文本域

private JButton FaSongJbt;

private JButton PingChuJbt;
//文本框
// private JTextField DiZhiText;

private JTextField ShuRuText;

private Object fromClient;

private JComboBox xiaComb;

private Properties ppt;

private JCheckBox chooseJbx;

private JTextField huiFuText;

public FeiChunFrame() {

ppt = new Properties();

try {
ppt.load(new FileInputStream("j124.properties"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// TODO Auto-generated constructor stub
//工具箱
Toolkit tk = Toolkit.getDefaultToolkit();
//窗体大小
this.setSize(400, 400);
int screenW = (int) tk.getScreenSize().getWidth();
int screenH = (int) tk.getScreenSize().getHeight();
this.setLocation((screenW - 400) / 2, (screenH - 400) / 2);
// 窗体大小是否可更改
this.setResizable(true);
//
this.setIconImage(tk.createImage("img/logo.GIF"));
// 标题
this.setTitle("我的飞春");
//
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 内容面板
this.addContent();
// 窗体可见性
this.setVisible(true);

jianTing();

}

public JComboBox getXiaComb() {
return xiaComb;
}

public void setXiaComb(JComboBox xiaComb) {
this.xiaComb = xiaComb;
}

public Properties getPpt() {
return ppt;
}

public void setPpt(Properties ppt) {
this.ppt = ppt;
}

private void addContent() {
// TODO Auto-generated method stub
//
this.contentP = this.getContentPane();
// 空布局
this.contentP.setLayout(null);

this.msgArea = new JTextArea();
this.setBackground(Color.WHITE);
this.msgArea.setEnabled(false);
JScrollPane sp = new JScrollPane(this.msgArea);
sp.setBounds(5, 5, 370, 270);
this.contentP.add(sp);

this.ShuRuText = new JTextField("");
this.ShuRuText.setBounds(10, 280, 100, 25);
this.contentP.add(this.ShuRuText);

this.FaSongJbt = new JButton("发送");
this.FaSongJbt.setBounds(120, 280, 60, 25);
this.contentP.add(this.FaSongJbt);
this.FaSongJbt.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=sdf.format(d);
String str=FeiChunFrame.this.ShuRuText.getText();
String IP=FeiChunFrame.this.ppt.getProperty(str);
// String sendIP = FeiChunFrame.this.ShuRuText.getText();
FeiChunFrame.this.msgArea.setText(time+"\n"+"我说"+str);
Socket client = null;
try {
client = new Socket(IP, 9527);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
bw.write("王海军&" + "&" + str);
bw.flush();

} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
if(client !=null){
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});

// this.DiZhiText = new JTextField("");
// this.DiZhiText.setBounds(190, 280, 80, 25);
// this.contentP.add(this.DiZhiText);

this.chooseJbx = new JCheckBox("自动回复");
this.chooseJbx.setBounds(10, 310, 100, 25);
this.contentP.add(this.chooseJbx);

this.huiFuText = new JTextField("");
this.huiFuText.setBounds(130, 310, 80, 25);
this.contentP.add(this.huiFuText);

this.xiaComb = new JComboBox(this.ppt.keySet().toArray());
this.xiaComb.setBounds(190, 280, 80, 25);
this.contentP.add(this.xiaComb);

this.PingChuJbt = new JButton("清除");
this.PingChuJbt.setBounds(280, 280, 60, 25);
this.contentP.add(this.PingChuJbt);
this.PingChuJbt.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
FeiChunFrame.this.msgArea.setText(null);
}
});
}

public void jianTing(){

ServerSocket server = null;

try {
server = new ServerSocket(9527);
while(true){
Socket fc = server.accept();

new MSGThread(fc, this).start();

}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(server != null){
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

public Container getContentP() {
return contentP;
}

public void setContentP(Container contentP) {
this.contentP = contentP;
}

public JTextArea getMsgArea() {
return msgArea;
}

public void setMsgArea(JTextArea msgArea) {
this.msgArea = msgArea;
}

public JButton getFaSongJbt() {
return FaSongJbt;
}

public void setFaSongJbt(JButton faSongJbt) {
FaSongJbt = faSongJbt;
}

public JButton getPingChuJbt() {
return PingChuJbt;
}

public void setPingChuJbt(JButton pingChuJbt) {
PingChuJbt = pingChuJbt;
}

// public JTextField getDiZhiText() {
// return DiZhiText;
// }
//
// public void setDiZhiText(JTextField diZhiText) {
// DiZhiText = diZhiText;
// }

public JTextField getShuRuText() {
return ShuRuText;
}

public void setShuRuText(JTextField shuRuText) {
ShuRuText = shuRuText;
}

public Object getFromClient() {
return fromClient;
}

public void setFromClient(Object fromClient) {
this.fromClient = fromClient;
}

}

用来监听消息

package com.lovo.feichun;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MSGThread extends Thread{

private Socket fromClient;
private FeiChunFrame feichun;

public MSGThread(Socket fromClient,FeiChunFrame feichun) {
// TODO Auto-generated constructor stub

this.fromClient = fromClient;
this.feichun = feichun;
}

public void run(){

BufferedReader br= null;
try {
br = new BufferedReader(new InputStreamReader(
fromClient.getInputStream()));
String str = br.readLine();
String[] allMsg = str.split("&");
// Date sendTime = new Date(Long.parseLong(allMsg[2]));
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// System.out.println(sdf.format(sendTime));
// System.out.println(allMsg[0] + "说:" + allMsg[1]);
this.getFeichun().getMsgArea().append(this.getFeichun().getMsgArea().getText()+"\n"+"\n"+allMsg[0]+"他说:"+allMsg[1]);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(this.fromClient != null){
try {
this.fromClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

public FeiChunFrame getFeichun() {
return feichun;
}

public void setFeichun(FeiChunFrame feichun) {
this.feichun = feichun;
}

}

main方法运行

package com.lovo.test;

import com.lovo.feichun.FeiChunFrame;

public class TestMain {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new FeiChunFrame();
}

}

自己都不知道怎么写完的

类似于QQ的简单的聊天代码的更多相关文章

  1. HTML QQ聊天代码 简单的一行代码

    简单的一行代码: <a href="tencent://message/?uin=173007740&Site=&Menu=yes">和17300774 ...

  2. 查看图片插件--Viewer(类似于qq和微信聊天 的查看图片)

    Viewer的github地址:https://github.com/fengyuanchen/viewer  下载该插件(在文件夹dist里面) 具有参考价值的几个网站:http://www.dow ...

  3. 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】

    说17号发超简单的教程就17号,qq核审通过后就封装了这个,现在放出来~~ 这个是我封装的一个开源项目:https://github.com/dunitian/LoTQQLogin ————————— ...

  4. 简单的聊天室代码php+swoole

    php swoole+websocket 客户端代码 <!DOCTYPE html> <html> <head> <title></title&g ...

  5. 微信公众平台中添加qq在线聊天代码

    微信公众平台是个不错的媒体,可以和你的小伙伴们即时交流,但你的小伙伴们是用手机上的微信,打字自然就慢了:有人说用微信网页版,那个也不习惯,再说也不一定所有人都知道网页版微信.(2014.01.22更新 ...

  6. Java网络编程以及简单的聊天程序

    网络编程技术是互联网技术中的主流编程技术之一,懂的一些基本的操作是非常必要的.这章主要讲解网络编程,UDP和Socket编程,以及使用Socket做一个简单的聊天软件. 全部代码下载:链接 1.网络编 ...

  7. C#编写简单的聊天程序

    这是一篇基于Socket进行网络编程的入门文章,我对于网络编程的学习并不够深入,这篇文章是对于自己知识的一个巩固,同时希望能为初学的朋友提供一点参考.文章大体分为四个部分:程序的分析与设计.C#网络编 ...

  8. 一个关于如何创建类似于QQ客户端聊天窗口的模拟小项目

    对于不久之前学习到的一个有关的类似于QQ聊天框的模拟项目,对其中涉及到的知识在这里做一下总结. 首先,你要先创建一个客户端聊天框(取名为:ChatClient,它是你创建的类),这个类继承了Frame ...

  9. 类似于qq空间类型的评论和回复

    最近学习thinkphp,做了一个博客系统,其中感觉实现一个类似于qq空间的评论和回复功能比较复杂,所以把这次的经历记录下来,与大家共勉,具体的方法就不说了,在这里分享一下思路. 目标就是这种,关键是 ...

随机推荐

  1. ORACLE 简化拼接

    and (in_sample.TEAM_GROUP = p_teamGroup or p_teamGroup is null);

  2. 动态sql语句基本语法--Exec与Exec sp_executesql 的区别

    http://www.cnblogs.com/goody9807/archive/2010/10/19/1855697.html 动态sql语句基本语法 1   :普通SQL语句可以用Exec执行   ...

  3. Rails 之微信开发 : OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

    微信公众平台,使用Ruby On Rails + Win7 在取得OpenID时,如果简单的使用http.get方法,会出现如下 SSL_connect returned=1 errno=0 stat ...

  4. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...

  5. MyBatis入门学习教程-调用存储过程

    一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 create table p_user( id int primary key auto_incr ...

  6. Hadoop总结篇之三---一个Job到底被提交到哪去了

    我们会定义Job,我们会定义map和reduce程序.那么,这个Job到底是怎么提交的?提交到哪去了?它到底和集群怎么进行交互的呢? 这篇文章将从头讲起. 开发hadoop的程序时,一共有三大块,也就 ...

  7. rsync命令详解

    介绍 rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部 ...

  8. Cassandra 介绍

    cassandra是一种NoSQL数据库,No是指No Relational.cassandra的数据模型结合了Dynamo的key/value和BigTable  的面向列的特点,主要被设计为存储大 ...

  9. Mybatis 批量更新 ORA-00911: 无效字符的错误

    使用<foreach></foreach> 批量insert时报错 ORA-00911: 无效字符的错误 <foreach collection="list&q ...

  10. CentOS7:安装Zabbix

    参考:CentOS 7 yum安装Zabbix 1. 安装Zabbix Server EPEL源里面有Zabbix的安装包,所以需要先安装EPEL. Zabbix源也可以从这里获得:http://re ...