先编辑页面

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. aspx aspx.cs

    http://www.cnblogs.com/axzxs2001/archive/2009/01/19/1378383.html

  2. undefined reference to `_init'问题解决

    今天利用CDT 的eclipse调试程序,遇到下面的问题: d:/plugin/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi ...

  3. [经验交流] Kubernetes Nginx Ingress 安装与使用

    Ingress 介绍 Kubernetes 上部署的微服务运行在它的私有网络中, 通过Pod实例的hostPort或Service实例的NodePort可以暴露到主机端口上,便于用户访问.但这样的方法 ...

  4. DNS错误 事件4000 4013

    DNS 错误事件4000 4013,无法创建活动目录本身的区域 WINDOWS 2003 DNS服务器无法解析 错误ID 4000 DNS 服务器无法打开 Active Directory.这台 DN ...

  5. 51nod 1237 最大公约数之和 V3

    求∑1<=i<=n∑1<=j<=ngcd(i,j) % P P = 10^9 + 7 2 <= n <= 10^10 这道题,明显就是杜教筛 推一下公式: 利用∑d ...

  6. CORBA IOR学习

    Interoperable Object References: IOR IOR用于表示一个对象引用,我们知道,当我们在客户端一个CORBA对象的时候,接触的并不是真正的对象,而是这个对象的代理(Pr ...

  7. opencv2.4.9在ubuntu(树莓派)上安装

    参考原文:https://my.oschina.net/u/1757926/blog/293976 1. 先从sourceforge上下载OpenCV的源码 http://jaist.dl.sourc ...

  8. Spark读取Hbase的数据

    val conf = HBaseConfiguration.create() conf.addResource(new Path("/opt/cloudera/parcels/CDH-5.4 ...

  9. linux socket编程实例

    /* ============================================================================ Name : client.c Auth ...

  10. MVC表单

    .cs namespace SendToMvcHello.Controllers { public class HomeController : Controller { // GET: Home p ...