一个聊天工具
本帖最后由 GH逐风 于 2017-8-21 22:02 编辑实现一个简单的靠IP地址聊天的工具
package com.gh.liaotianshi;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
public class LiaoTianShi extends Frame{
private TextArea faSongKuang;
private TextArea xianShiKuang;
private TextField tf;
private Button faSong;
private Button jiLu;
private Button qingPing;
private Button zhenDong;
private DatagramSocket socket;
private BufferedWriter bw;
/**
* @param args
*/
public static void main(String[] args) {
new LiaoTianShi();
}
publicLiaoTianShi() {
windows();
xiaPanel();
shangPanel();
new jieShou().start();
try {
socket = new DatagramSocket();
bw = new BufferedWriter(new FileWriter("data.txt", true));
} catch (Exception e) {
e.printStackTrace();
}
event();
}
/*
* 创建一个按钮事件
* 1.X按钮,退出jvm;
* 2.发送按钮,发送在发送框输入的消息
* 3.聊天记录按钮,将数据库的聊天记录在显示框显示
* 4.清屏按钮,清空显示框
* 5.震动,发送给对方一个震动
*/
private void event() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
try {
bw.close(); //关闭 BufferedWriter
socket.close(); //关闭 DatagramSocket
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0); //退出jvm
}
});
faSong.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
faSong();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
jiLu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
data();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
qingPing.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
qingPing();
}
});
zhenDong.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
faSong(new byte[]{-1}, tf.getText());
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
faSongKuang.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
try {
faSong();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
}
private void zhenDong() {
int x = this.getLocation().x; //获取x坐标
int y = this.getLocation().y; //获取y坐标
//改变坐标位置达到震动效果
for(int i = 0;i < 50;i++) {
this.setLocation(x + 15, y + 5);
this.setLocation(x - 15, y - 5);
this.setLocation(x + 15, y - 5);
this.setLocation(x - 15, y + 5);
this.setLocation(x, y);
}
}
private void qingPing() {
xianShiKuang.setText(""); //清空显示框
}
private void data() throws IOException {
bw.flush(); //刷新缓存
FileInputStream fis = new FileInputStream("data.txt"); //读取数据库文件
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //创建内存缓冲区
int len;
byte[] arr = new byte;
while((len = fis.read(arr)) != -1) { //写出数据库文件到缓冲区
baos.write(arr, 0, len);
}
String s = baos.toString(); //将字节转换为字符串
xianShiKuang.setText(s); //将字符串显示到显示框
baos.close();
fis.close();
}
private void faSong() throws Exception {
String xiaoXi = faSongKuang.getText(); //获取发送框中输入的消息
String ip = tf.getText(); //获取输入的IP地址
faSong(xiaoXi.getBytes(), ip);
String time = getTime(); //获取当前时间
String str = time + " " + ip + " 说:\r\n" + xiaoXi + "\r\n\r\n";
xianShiKuang.append(str); //将字符串添加到显示框
bw.write(str); //将发送的消息添加进数据库
faSongKuang.setText(""); //清空发送框
}
public void faSong(byte[] arr, String ip) throws Exception {
DatagramPacket packet = //确定发送的消息,ip,端口
new DatagramPacket(arr, arr.length, InetAddress.getByName(ip),6969);
socket.send(packet); //发送消息
}
private String getTime() { //获取当前时间的方法
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年DD月mm日 HH:mm:ss");
return sdf.format(d);
}
private void shangPanel() {
Panel p = new Panel(); //创建panel
xianShiKuang = new TextArea(); //创建文本框
faSongKuang = new TextArea(5,1);
p.setLayout(new BorderLayout()); //设置布局管理器
p.add(xianShiKuang,BorderLayout.CENTER); //将文本开框添加到panel
p.add(faSongKuang,BorderLayout.SOUTH);
xianShiKuang.setEditable(false); //设置显示框不可写
xianShiKuang.setBackground(Color.WHITE); //设置显示框的颜色
xianShiKuang.setFont(new Font("jj", Font.PLAIN, 15)); //设置字体
faSongKuang.setFont(new Font("jj", Font.PLAIN, 15));
this.add(p, BorderLayout.CENTER); //将panel添加到frame
}
public void xiaPanel() {
Panel p = new Panel();
tf = new TextField(15); //创建ip输入文本框
faSong = new Button("发 送"); //创建按钮
jiLu = new Button("聊天记录");
qingPing = new Button("清 屏");
zhenDong = new Button("震 动");
p.add(tf); //将按钮添加到panel
tf.setText("127.0.0.1");
p.add(faSong);
p.add(jiLu);
p.add(qingPing);
p.add(zhenDong);
this.add(p,BorderLayout.SOUTH); //将panel添加到frame
}
public void windows() {
this.setSize(400,600); //设置窗体大小
this.setLocation(500, 60); //设置窗体位置
this.setIconImage(Toolkit.getDefaultToolkit().createImage("GH.png")); //设置图标
this.setVisible(true); //设置窗体可见
this.setTitle("吾爱聊天室"); //设置名字
}
public class jieShou extends Thread {
public void run() {
try {
DatagramSocket socket = new DatagramSocket(6969);
DatagramPacket packet = new DatagramPacket(new byte, 1024 * 8);
while(true) {
socket.receive(packet);
byte[] arr = packet.getData();
int len = packet.getLength();
if(arr == -1 && len == 1) {
zhenDong();
continue;
}
String s = new String(arr,0, len);
String time = getTime();
String ip = packet.getAddress().getHostAddress();
String str = time + " " + ip + " 说:\r\n" + s + "\r\n\r\n";
xianShiKuang.append(str);
bw.write(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
https://www.52pojie.cn/forum.php?mod=image&aid=938871&size=300x300&key=57598a97d07b339e&nocache=yes&type=fixnone&ramdom=cjvxH
弄了一个jar文件,安装了jvm的可以把聊天室运行起来
屌丝通道链接: https://pan.baidu.com/s/1skXgvQP 密码: vymr
土豪通道:
谢谢分享。持续关注,别失望啊 谢谢分享,持续关注。 这是简易聊天室吧好像 厉害了我的哥 厉害了我的哥 java写的但是貌似这个 拿到了也不能用不过可以借鉴思路 可以很可以啊 大神,有没有恢复聊天记录的软件的?
页:
[1]
2