类似于QQ的简单的聊天代码
先编辑页面
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的简单的聊天代码的更多相关文章
- HTML QQ聊天代码 简单的一行代码
简单的一行代码: <a href="tencent://message/?uin=173007740&Site=&Menu=yes">和17300774 ...
- 查看图片插件--Viewer(类似于qq和微信聊天 的查看图片)
Viewer的github地址:https://github.com/fengyuanchen/viewer 下载该插件(在文件夹dist里面) 具有参考价值的几个网站:http://www.dow ...
- 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】
说17号发超简单的教程就17号,qq核审通过后就封装了这个,现在放出来~~ 这个是我封装的一个开源项目:https://github.com/dunitian/LoTQQLogin ————————— ...
- 简单的聊天室代码php+swoole
php swoole+websocket 客户端代码 <!DOCTYPE html> <html> <head> <title></title&g ...
- 微信公众平台中添加qq在线聊天代码
微信公众平台是个不错的媒体,可以和你的小伙伴们即时交流,但你的小伙伴们是用手机上的微信,打字自然就慢了:有人说用微信网页版,那个也不习惯,再说也不一定所有人都知道网页版微信.(2014.01.22更新 ...
- Java网络编程以及简单的聊天程序
网络编程技术是互联网技术中的主流编程技术之一,懂的一些基本的操作是非常必要的.这章主要讲解网络编程,UDP和Socket编程,以及使用Socket做一个简单的聊天软件. 全部代码下载:链接 1.网络编 ...
- C#编写简单的聊天程序
这是一篇基于Socket进行网络编程的入门文章,我对于网络编程的学习并不够深入,这篇文章是对于自己知识的一个巩固,同时希望能为初学的朋友提供一点参考.文章大体分为四个部分:程序的分析与设计.C#网络编 ...
- 一个关于如何创建类似于QQ客户端聊天窗口的模拟小项目
对于不久之前学习到的一个有关的类似于QQ聊天框的模拟项目,对其中涉及到的知识在这里做一下总结. 首先,你要先创建一个客户端聊天框(取名为:ChatClient,它是你创建的类),这个类继承了Frame ...
- 类似于qq空间类型的评论和回复
最近学习thinkphp,做了一个博客系统,其中感觉实现一个类似于qq空间的评论和回复功能比较复杂,所以把这次的经历记录下来,与大家共勉,具体的方法就不说了,在这里分享一下思路. 目标就是这种,关键是 ...
随机推荐
- OC宏和常量
1.通常常量的定义const放在最前面: 2.宏的定义 #define TAG_ID 101,注意:宏名称和值之间没有等号,宏定义的末尾也不需要分号
- Turn off Debug Logging in Quartz .Net
Quartz.net uses Common.Logging, so something like this in your App.config/Web.config: <configSect ...
- 如何在ubuntu里面关掉后台的meteor
kill -9 `ps ax | grep node | grep meteor | awk '{print $1}'` 或者远程终止服务器里面的meteor ssh [user]@[server] ...
- 使用Grunt 插件打包Electron Windows应用
最近利用Electron来创建跨桌面应用的趋势似乎很火.看了几个用Electron开发的应用,这些应用在windows下面的安装方式,都是类似一个绿色软件的安装方法,下载.zip->解压到相应目 ...
- WinRAR压缩操作帮助类
//------------------------------------------------------------------------------------- // All Right ...
- XML中& <> 单引号' 双引号 " 报错
由于xml中 这些字符是特殊字符,所以把&改成& 就行了 ,注意后面一定要带一个分号; < < 小于号> > ...
- 跨平台编程:关于VS和QT那些事
1.Win平台 Qt5.7 for Win32 (VS2013) 编辑器:Qt Creator 4.0 编译器:MSVC12 for X86 (cl.exe&link.exe) 调试器:CDB ...
- Codeforces 732F [边双联通分量][tarjan]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给一个无向图.现在要求给这个无向图的边加上方向. 定义f(x)为从x点出发能够到达的点的数目. 使得MIN(f(x))最大. 思路: 先tarja ...
- Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)
1.写入Excel,并加密压缩.不保存文件 String dcxh = String.format("%03d", keyValue); String folderFileName ...
- Create a geoprocessing tool to buffer a layer and retrieve messages____sync
using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropSer ...