代码在这,完整版压缩包放附件,觉得好久点赞啊!
package zhiqiu;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
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.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
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.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ChuangTi {
private static String ip;
private static JFrame f;
private static Button b1;
private static Button b2;
private static Button b3;
private static Button b4;
private static Button b5;
private static JTextArea xs;
private static TextArea xr;
private static TextField tf;
private static DatagramSocket so;
private static BufferedWriter bw;
private static ImageIcon imageIcon;
public static class receive extends Thread {
private String s;
// 定义接收端
@Override
public void run() {
try {
so = new DatagramSocket(9999);
bw = new BufferedWriter(new FileWriter("jilu.txt", true));
DatagramPacket dp = new DatagramPacket(new byte[8192], 8192);
while (true) {
// 接收信息
so.receive(dp);
// 获取字节数据
byte[] arr = dp.getData();
// 获取有效长度
int len = dp.getLength();
// 判断是否震动
if (arr[0] == -1 && len == 1) {
// 调用震动方法
zhendong();
// 收到震动指令后,下面的代码不需要执行,结束本次循环,继续下次循环
//System.out.println(arr[0]);
continue;
}
// 转换成字符串
s = new String(arr, 0, len);
// 获取当前时间
final String time = gettime();
// 获取发送者的名字
final String name = InetAddress.getLocalHost().getHostName();
String str = name + "(" + time + ")" + "\n" + s + "\n";
xs.append(str);
bw.write(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
f = new JFrame("知秋");
// 设置窗体大小
f.setSize(520, 800);
// 设置窗体位置
f.setLocation(900, 200);
// 设置图标
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage("jiqimao.png");
imageIcon = new ImageIcon("3.jpg");
xs = new JTextArea() {
Image image = imageIcon.getImage();
{
setOpaque(false);
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
super.paint(g);
}
};
JScrollPane scrollPane = new JScrollPane(xs);
Container content = f.getContentPane();
content.add(scrollPane, BorderLayout.CENTER);
// 创建中间的panel
centerpanel(f);
f.setIconImage(image);
// 创建南边的panel
southpanel(f);
// 设置关闭按钮
f.addWindowListener(new guanbi());
// 给按钮添加对应的功能
anniu(f);
// 设置窗口可视化
f.setVisible(true);
so = new DatagramSocket();
new receive().start();
}
public static void anniu(Frame f) {
// 给按钮b1添加功能,鼠标监听
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
send();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
getjilu();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xs.setText("");
}
});
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
zhendong();
try {
send(new byte[] { -1 }, tf.getText());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
b5.addActionListener(new ActionListener() {
@Override
// 释放监听
public void actionPerformed(ActionEvent e) {
try {
bw.close();
// so.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
}
});
// 添加键盘监听
xr.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
try {
send();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
private static void zhendong() {
int x = f.getLocation().x;
int y = f.getLocation().y;
for (int i = 0; i < 20; i++) {
f.setLocation(x + 20, y);
f.setLocation(x + 20, y + 20);
f.setLocation(x - 20, y);
f.setLocation(x, y - 20);
f.setLocation(x, y);
}
}
public static void getjilu() throws Exception {
bw.flush();
FileInputStream fi = new FileInputStream("jilu.txt");
// 在内存中创建缓冲区
ByteArrayOutputStream bao = new ByteArrayOutputStream();
byte[] arr = new byte[8192];
int len;
while ((len = fi.read(arr)) != -1) {
bao.write(arr, 0, len);
}
String s = bao.toString();
xs.setText(s);
fi.close();
}
public static void send(byte[] arr, String s) throws IOException {
// 包装发送信息
DatagramPacket dp = new DatagramPacket(arr, arr.length, InetAddress.getByName(ip), 9999);
// 发送数据
so.send(dp);
}
public static void send() throws IOException {
// 获取发送区域内容
String xx = xr.getText();
// 获取ip地址
ip = tf.getText();
ip = ip.trim().length() == 0 ? "255.255.255.255" : ip;
send(xx.getBytes(), ip);
// 获取当前时间
final String time = gettime();
// 获取发送者的名字
final String name = InetAddress.getLocalHost().getHostName();
// 显示在显示区域
String str = name + "(" + time + ")" + "\n" + xx + "\n";
xs.append(str);
bw.write(str);
// 写入区域清空
xr.setText("");
}
public static String gettime() {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(d);
}
public static void centerpanel(JFrame f) {
// 创建中间的panel
Panel senter = new Panel();
// 设置显示区域为不可写入
xs.setEditable(false);
// 设置显示区域背景颜色
xs.setBackground(Color.white);
// 设置字体的大小
xs.setFont(new Font("xxx", Font.PLAIN, 20));
xs.setForeground(Color.RED);
xr = new TextArea(5, 1);
// 设置字体的大小
xr.setFont(new Font("xxx", Font.PLAIN, 20));
xr.setForeground(Color.BLACK);
// 设置为边界布局管理器
senter.setLayout(new BorderLayout());
// 添加文本区域
senter.add(xr, BorderLayout.SOUTH);
// 放置在中间
senter.add(xs, BorderLayout.CENTER);
// 将senter放置在窗体的中间
f.add(senter, BorderLayout.CENTER);
}
public static void southpanel(Frame f) {
// 设置南边的panel
Panel south = new Panel();
tf = new TextField(15);
b1 = new Button("发送");
b2 = new Button("记录");
b3 = new Button("清屏");
b4 = new Button("震动");
b5 = new Button("关闭");
tf.setText("192.168.0.110");
// 添加按钮
south.add(b1);
south.add(b2);
south.add(b3);
south.add(b4);
south.add(b5);
south.add(tf);
// 添加到窗口南边
f.add(south, BorderLayout.SOUTH);
}
}
// 设置窗口关闭监听按钮
class guanbi extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
|
|