类似于QQ的简单的聊天代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类似于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的简单的聊天代码的主要内容,如果未能解决你的问题,请参考以下文章

Android实现类似QQ聊天的功能 怎么实现

AndroidRichText 让Textview轻松的支持富文本(图像ImageSpan点击效果等等类似QQ微信聊天)

怎么利用Python做一个即时通信软件呢,类似于QQ,能实现基本的聊天和文件传输功能?

一款基于python的安全即时通讯系统源码,实现类似于QQ的聊天软件

css--画一个带有边框的三角形(类似于QQ的聊天框)

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