package index.JPanelMoudle;
|
|
import baowen.Dell_Baowen;
|
import tools.GetNowTime;
|
import tools.Tools;
|
|
import javax.swing.*;
|
import javax.swing.border.TitledBorder;
|
import java.awt.*;
|
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseEvent;
|
import java.nio.charset.Charset;
|
|
public class showDataComp {
|
|
private static JScrollPane dataShow;//解析数据滚动面板
|
private static JTextArea textArea1;
|
private static JPopupMenu popupMenu;//右键菜单列表
|
static boolean time=true;//显示时间戳
|
static boolean ascll=false;//ascll显示
|
|
// 获取 textArea1 文本区,若为空则创建
|
public static JTextArea getTextArea1() {
|
if (textArea1 == null) {
|
textArea1 = new JTextArea(); // 初始化该文本区
|
textArea1.addMouseListener(new MouseAdapter() {
|
@Override
|
public void mousePressed(MouseEvent e) {
|
maybeShowPopup(e);
|
}
|
|
@Override
|
public void mouseReleased(MouseEvent e) {
|
maybeShowPopup(e);
|
}
|
|
// 显示右键菜单
|
private void maybeShowPopup(MouseEvent e) {
|
if (e.isPopupTrigger()) { // 判断是否是右键点击
|
getPopupMenu().show(e.getComponent(), e.getX(), e.getY());
|
}
|
}
|
});
|
}
|
return textArea1;
|
}
|
// 获取 解析数据滚动面板,若为空则创建
|
public static JScrollPane getDataShow() {
|
if (dataShow == null) {
|
dataShow = new JScrollPane(); // 初始化该滚动面板
|
dataShow.setBorder(new TitledBorder("接收数据解析"));
|
}
|
return dataShow;
|
}
|
|
public static JPopupMenu getPopupMenu(){
|
if (popupMenu == null) {
|
popupMenu = new JPopupMenu();
|
popupMenu.setBackground(new Color(245, 245, 245)); // 设置菜单背景色
|
popupMenu.setFont(new Font("Segoe UI", Font.PLAIN, 14)); // 设置字体
|
|
// 动态计算菜单项的文本
|
String showAscll = ascll ? "HEX显示" : "ASCLL显示";
|
String noShowtime = time ? "隐藏时间戳" : "显示时间戳";
|
|
// 创建菜单项
|
JMenuItem clear = new JMenuItem("清空窗口");
|
JMenuItem ascllShow = new JMenuItem(showAscll);
|
JMenuItem timeNoShow = new JMenuItem(noShowtime);
|
|
// 添加动作监听器
|
clear.addActionListener(e -> textArea1.setText(""));
|
ascllShow.addActionListener(e -> setAscll());
|
timeNoShow.addActionListener(e -> setTimeShow());
|
|
// 鼠标悬停时改变背景颜色
|
clear.addMouseListener(createMouseAdapter(clear));
|
ascllShow.addMouseListener(createMouseAdapter(ascllShow));
|
timeNoShow.addMouseListener(createMouseAdapter(timeNoShow));
|
|
// 添加菜单项到菜单
|
popupMenu.add(clear);
|
popupMenu.addSeparator();
|
popupMenu.add(ascllShow);
|
popupMenu.addSeparator();
|
popupMenu.add(timeNoShow);
|
} else {
|
// 如果popupMenu已经创建,更新菜单项
|
// 动态更新菜单项文本
|
((JMenuItem) popupMenu.getComponent(2)).setText(ascll ? "HEX显示" : "ASCLL显示");
|
((JMenuItem) popupMenu.getComponent(4)).setText(time ? "隐藏时间戳" : "显示时间戳");
|
}
|
|
return popupMenu;
|
}
|
|
// 鼠标悬停时改变背景颜色的通用方法
|
private static MouseAdapter createMouseAdapter(JMenuItem item) {
|
return new MouseAdapter() {
|
@Override
|
public void mouseEntered(MouseEvent e) {
|
item.setBackground(new Color(220, 220, 220)); // 设置悬停时的颜色
|
}
|
|
@Override
|
public void mouseExited(MouseEvent e) {
|
item.setBackground(new Color(240, 240, 240)); // 恢复正常颜色
|
}
|
};
|
}
|
|
|
public static void addAreaTextMes(byte[] Datas,String ip,int port) {
|
String message = ascll ? new String(Datas, Charset.forName("GBK")): Tools.Bytes2HexString(Datas);
|
if (time) {
|
getTextArea1().append(GetNowTime.timestamp2() + ":收←" + message + "\n");
|
}else {
|
getTextArea1().append( message + "\n");
|
}
|
if (port!=9999) {
|
Dell_Baowen.intsert(ip, message, GetNowTime.timestamp2(), port);
|
}
|
// 获取文本区域的长度(即最后一行的结束位置)
|
int length = textArea1.getDocument().getLength();
|
// 设置光标位置到文本的末尾
|
textArea1.setCaretPosition(length);
|
}
|
|
public static void addSendAreaTextMes(byte[] Datas){
|
String message = ascll ? new String(Datas, Charset.forName("GBK")): Tools.Bytes2HexString(Datas);
|
if (time) {
|
getTextArea1().append(GetNowTime.timestamp2()+":发→"+message+"\n");
|
}else {
|
getTextArea1().append( message + "\n");
|
}
|
// 获取文本区域的长度(即最后一行的结束位置)
|
int length = textArea1.getDocument().getLength();
|
// 设置光标位置到文本的末尾
|
textArea1.setCaretPosition(length);
|
}
|
|
public static void setTimeShow(){
|
if (time){
|
time=false;
|
}else {
|
time=true;
|
}
|
}
|
|
public static void setAscll(){
|
if (ascll){
|
ascll=false;
|
}else {
|
ascll=true;
|
}
|
}
|
|
}
|