package com;
//飞秋
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
public class ListFrame extends JFrame {
private JList list = null; // 列表界面
private JLabel showMessLabel = null; // 显示信息
private JButton functionBtn = null; // 功能按钮
private JLabel showLabel = null; // 提示联机人数
private JButton sendBtn = null; // 发送按钮
private JButton refreshBtn = null; // 刷新按钮
private JTextArea contentArea = null; // 发送内容
private List<Students> students = null; // 用户信息
private Border border = BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1); // 设置边框
private static DatagramSocket ds = null; //
private final static int PORT = 2995; // 软件端口号
private AcceptListenerThread acceptThread = null; // 接收数据监听线程
private DatagramPacket recPacket = null; // 数据报包
private String name = null; // 本机用户名
private String hostName = null; // 本机主机名
private SystemTray systemTray = null; // 系统托盘区
private TrayIcon trayIcon = null; // 托盘图标
private Image img1 = null; // 图标1
private Image img2 = null; // 图标2
private GregorianCalendar gc = null; // 接收消息的时间
private InetAddress allInetAddress = null; // 所有子网InetAddress
private List<Message> messageList = null; // 待处理消息队列
private UpdateTrayIcon updateThread = null; // 图标闪烁线程
private int number = 0; // 联机人数
private ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendBtn) {
// 发送按钮 发送聊天内容
sendChatMessage();
} else if (e.getSource() == refreshBtn) {
// 刷新按钮 群发udp消息至局域网的所有机器并处理应答消息 1
sendMessage(name, hostName, 1, null, allInetAddress);
}
}
};
public JList getList() {
return this.list;
}
public List<Students> getStudents() {
return this.students;
}
public JTextArea getContentArea() {
return this.contentArea;
}
/**
* 发送聊天内容
* */
private void sendChatMessage() {
// 得到选中的主机名
Students selStudent = (Students) list.getSelectedValue();
InetAddress inetAddress = null;
if (selStudent != null) {
try {
inetAddress = InetAddress.getByName(selStudent.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
// 向选中的主机名发送消息内容 4 聊天消息
String content = contentArea.getText().trim();
if (content.length() > 0) {
sendMessage(name, hostName, 4, content, inetAddress);
}
contentArea.setText("");
} else {
JOptionPane.showMessageDialog(ListFrame.this, "请选择接收消息的主机");
}
}
// 窗口关闭事件
/*
* private WindowListener windowListener = new WindowAdapter() { public void
* windowClosing(WindowEvent e) {
*
* }; };
*/
public ListFrame() {
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
// this.addWindowListener(windowListener);
// 初始化本机信息
initLocalInfo();
// 初始化组件
initFrame();
// 布局
JPanel panel = setFrameLayout();
this.add(panel);
this.pack();
// 给系统添加托盘
addTrayIcon();
try {
ds = new DatagramSocket(PORT);
} catch (SocketException e) {
e.printStackTrace();
}
// 发送上线消息
sendOnlineMessage();
// 接收的数据报存入的包
byte[] buf = new byte[3072];
recPacket = new DatagramPacket(buf, buf.length);
// 启动接收监听方法
startListener();
}
/**
* 添加系统托盘区 以及相应事件
* */
private void addTrayIcon() {
// 图标初始化
Toolkit toolkit = Toolkit.getDefaultToolkit();
img1 = toolkit.getImage("img/fengjie1.jpg");
img2 = toolkit.getImage("img/tom.jpg");
if (SystemTray.isSupported()) {
systemTray = SystemTray.getSystemTray();
PopupMenu pm = new PopupMenu();
MenuItem exitMenuItem = new MenuItem("退出程序");
exitMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 给所有的发送消息 8 下线
sendMessage(name, hostName, 8, null, allInetAddress);
acceptThread.stopThread(); // 停掉消息监听线程
// 停掉图标闪烁线程
if (updateThread != null && updateThread.isAlive()) {
updateThread.stopThread();
}
if (ds != null) {
if (!ds.isClosed()) {
ds.close();
}
}
ListFrame.this.setVisible(false);
if (systemTray != null && trayIcon != null) {
systemTray.remove(trayIcon);
}
System.exit(0);
}
});
/**
* 设置系统信息
*
* */
MenuItem setMenuItem = new MenuItem("设置");
setMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetFrame(ListFrame.this).setVisible(true);
}
});
pm.add(setMenuItem);
pm.addSeparator();
pm.add(exitMenuItem);
trayIcon = new TrayIcon(img1, "范范飞鸽", pm);
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// super.mouseClicked(e);
if (e.getButton() == MouseEvent.BUTTON1) {
// 左击
if (e.getClickCount() == 1) {
// 单击 检查待处理消息队列,若队列不为空则取出第一条消息并显示在【收到消息】界面中
if (messageList != null && messageList.size() > 0) {
new MessageFrame(messageList.get(0),
ListFrame.this).setVisible(true);
messageList.remove(0);
}
if (messageList != null && messageList.size() == 0) {
// 消息队列无消息,取消闪烁
updateThread.setFlag();
updateThread.stopThread();
}
} else if (e.getClickCount() == 2) {
// 双击 用户双击托盘区图标,显示软件主界面
ListFrame.this.setVisible(true);
}
}
}
});
try {
systemTray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
/**
* 初始化本机信息
*
* */
private void initLocalInfo() {
students = new LinkedList<Students>();
messageList = new LinkedList<Message>();
name = "范涛"; // 本机用户名
try {
hostName = InetAddress.getLocalHost().getHostName(); // 本机主机名
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
students.add(new Students(name, null, hostName));
// 得到本子网所有InetAddre
String addString = null;
String inetString = null;
try {
// 得到本地ip
addString = InetAddress.getLocalHost().getHostAddress();
// 得到子网ip地址
if (addString != null) {
String[] addStrs = addString.split("[.]");
inetString = addStrs[0] + "." + addStrs[1] + "." + addStrs[2]
评论0