zsh_root
2025-01-06 7857a444de69124e9f7fb45f98b0ae818b107f23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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(-> textArea1.setText(""));
            ascllShow.addActionListener(-> setAscll());
            timeNoShow.addActionListener(-> 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;
        }
    }
 
}