先编辑页面

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. MyBatis入门学习教程-MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  2. Android学习笔记——权限解释

    <!--允许读取电话状态SIM的权限--><uses-permission android:name="android.permission.READ_PHONE_STAT ...

  3. 记一次故障处理----主机异常关闭后mongodb二进制文件损坏

    今天,在某个演示环境中,我们的产品经历过整个机房断电后,出现了mongodb二进制文件损坏,以下是故障的分析记录过程: 1.在客户处支撑的同事发现整个机房断电再恢复后,3个mongodb复制集中,有1 ...

  4. java基础-servlet-1

    servlet,是运行于服务器端的小程序.它既可以运行在http服务器端,也可以运行在mail等其他服务器端.我是这样理解,tomcat把接收到的客户端请求,转发给servlet,它负责业务处理,并且 ...

  5. 【学】AngularJS日记(3)- $apply(), run()方法

    $scope.$apply()方法可以强制$apply()里运行的函数所改变的model里的数据直接反应到view里,因为在angular的环境中,有时会用到原生js或者jquery的时候,这些行为有 ...

  6. 在macos上利用vmware fusion安装Ubuntu

    1. 安装vmware fusion http://www.vmware.com/products/fusion 下载以后,可以在网上找注册码,最好下载最新的,这里下载的是7的版本 2. 下载ubun ...

  7. 快速开发一个PHP电影爬虫

    今天来做一个PHP电影小爬虫.我们来利用simple_html_dom的采集数据实例,这是一个PHP的库,上手很容易.simple_html_dom 可以很好的帮助我们利用php解析html文档.通过 ...

  8. codeforces 83 D. Numbers

    题意: 给出l,r,k,(1 ≤ l ≤ r ≤ 2·109, 2 ≤ k ≤ 2·109) 求在区间[l,r]内有多少个数i满足 k | i,且[2,k-1]的所有数都不可以被i整除 首先,如果k不 ...

  9. JVM实用参数(四)内存调优

    理想的情况下,一个Java程序使用JVM的默认设置也可以运行得很好,所以一般来说,没有必要设置任何JVM参数.然而,由于一些性能问题(很不幸的是,这些问题经常出现),一些相关的JVM参数知识会是我们工 ...

  10. JS设计模式--简单工厂模式

    在JS中创建对象会习惯的使用new关键字和类构造函数(也是可以用对象字面量). 工厂模式就是一种有助于消除两个类依赖性的模式. 工厂模式分为简单工厂模式和复杂工厂模式,这篇主要讲简单工厂模式. 简单工 ...