826220679@qq.com
3 天以前 b518f895dec5264fd25e22a68300c40ceba6f43d
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package zhuye;
 
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
/**
 * 复制按钮工具类
 * 提供统一的复制按钮创建方法,默认显示复制图标,复制成功后显示成功图标
 */
public final class Fuzhibutton {
    
    // 默认图标大小
    private static final int DEFAULT_ICON_SIZE = 18;
    // 成功图标显示时长(毫秒)
    private static final int SUCCESS_ICON_DURATION = 1500;
    
    private Fuzhibutton() {
        // 工具类不需要实例化
    }
    
    /**
     * 创建复制按钮
     * 默认显示 image/fuzhi.png 图标,复制成功后显示 image/fuzhisucc.png 图标
     * 
     * @param textToCopy 要复制的文本内容(如果为null,则需要在点击时通过回调提供)
     * @param tooltip 按钮提示文本
     * @param hoverColor 鼠标悬停时的背景颜色(如果为null,使用默认颜色)
     * @return 配置好的复制按钮
     */
    public static JButton createCopyButton(String textToCopy, String tooltip, Color hoverColor) {
        JButton copyButton = new JButton();
        
        // 加载图标
        ImageIcon copyIcon = loadIcon("image/fuzhi.png", DEFAULT_ICON_SIZE, DEFAULT_ICON_SIZE);
        ImageIcon successIcon = loadIcon("image/fuzhisucc.png", DEFAULT_ICON_SIZE, DEFAULT_ICON_SIZE);
        
        final ImageIcon finalCopyIcon = copyIcon;
        final ImageIcon finalSuccessIcon = successIcon;
        
        // 设置按钮图标
        if (copyIcon != null) {
            copyButton.setIcon(copyIcon);
        } else {
            copyButton.setText("复制");
        }
        
        // 设置按钮样式
        copyButton.setFont(new Font("微软雅黑", Font.PLAIN, 11));
        copyButton.setForeground(new Color(46, 139, 87)); // PRIMARY_COLOR
        copyButton.setBorder(BorderFactory.createEmptyBorder());
        copyButton.setContentAreaFilled(false);
        copyButton.setFocusPainted(false);
        copyButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        
        // 设置提示文本为"复制信息"
        copyButton.setToolTipText("复制信息");
        
        // 设置悬停效果
        final Color finalHoverColor = hoverColor != null ? hoverColor : new Color(230, 250, 240);
        copyButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                copyButton.setOpaque(true);
                copyButton.setBackground(finalHoverColor);
            }
            
            public void mouseExited(MouseEvent e) {
                copyButton.setOpaque(false);
            }
        });
        
        // 添加复制功能
        copyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = textToCopy;
                
                // 如果 textToCopy 为 null,尝试从父组件获取文本(如 JTextArea)
                if (text == null) {
                    // 尝试从父组件中查找 JTextArea
                    Container parent = copyButton.getParent();
                    while (parent != null) {
                        if (parent instanceof JDialog) {
                            Component[] components = ((JDialog) parent).getContentPane().getComponents();
                            text = findTextFromComponents(components);
                            break;
                        } else if (parent instanceof JFrame) {
                            Component[] components = ((JFrame) parent).getContentPane().getComponents();
                            text = findTextFromComponents(components);
                            break;
                        }
                        parent = parent.getParent();
                    }
                }
                
                // 如果没有需要复制的内容,直接返回,不显示弹窗提示
                if (text == null || text.trim().isEmpty() || "-1".equals(text.trim())) {
                    return;
                }
                
                try {
                    StringSelection selection = new StringSelection(text);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, selection);
                    
                    // 复制成功后切换为成功图标
                    if (finalSuccessIcon != null) {
                        copyButton.setIcon(finalSuccessIcon);
                        // 1.5秒后恢复为原始图标
                        Timer timer = new Timer(SUCCESS_ICON_DURATION, evt -> {
                            if (finalCopyIcon != null) {
                                copyButton.setIcon(finalCopyIcon);
                            }
                        });
                        timer.setRepeats(false);
                        timer.start();
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(
                        SwingUtilities.getWindowAncestor(copyButton),
                        "复制失败: " + ex.getMessage(),
                        "错误",
                        JOptionPane.ERROR_MESSAGE
                    );
                }
            }
        });
        
        return copyButton;
    }
    
    /**
     * 创建复制按钮(使用回调函数提供要复制的文本)
     * 
     * @param copyAction 复制动作回调,返回要复制的文本
     * @param tooltip 按钮提示文本
     * @param hoverColor 鼠标悬停时的背景颜色(如果为null,使用默认颜色)
     * @return 配置好的复制按钮
     */
    public static JButton createCopyButton(java.util.function.Supplier<String> copyAction, String tooltip, Color hoverColor) {
        JButton copyButton = new JButton();
        
        // 加载图标
        ImageIcon copyIcon = loadIcon("image/fuzhi.png", DEFAULT_ICON_SIZE, DEFAULT_ICON_SIZE);
        ImageIcon successIcon = loadIcon("image/fuzhisucc.png", DEFAULT_ICON_SIZE, DEFAULT_ICON_SIZE);
        
        final ImageIcon finalCopyIcon = copyIcon;
        final ImageIcon finalSuccessIcon = successIcon;
        
        // 设置按钮图标
        if (copyIcon != null) {
            copyButton.setIcon(copyIcon);
        } else {
            copyButton.setText("复制");
        }
        
        // 设置按钮样式
        copyButton.setFont(new Font("微软雅黑", Font.PLAIN, 11));
        copyButton.setForeground(new Color(46, 139, 87)); // PRIMARY_COLOR
        copyButton.setBorder(BorderFactory.createEmptyBorder());
        copyButton.setContentAreaFilled(false);
        copyButton.setFocusPainted(false);
        copyButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        
        // 设置提示文本为"复制信息"
        copyButton.setToolTipText("复制信息");
        
        // 设置悬停效果
        final Color finalHoverColor = hoverColor != null ? hoverColor : new Color(230, 250, 240);
        copyButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                copyButton.setOpaque(true);
                copyButton.setBackground(finalHoverColor);
            }
            
            public void mouseExited(MouseEvent e) {
                copyButton.setOpaque(false);
            }
        });
        
        // 添加复制功能
        copyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = copyAction != null ? copyAction.get() : null;
                
                // 如果没有需要复制的内容,直接返回,不显示弹窗提示
                if (text == null || text.trim().isEmpty() || "-1".equals(text.trim())) {
                    return;
                }
                
                try {
                    StringSelection selection = new StringSelection(text);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, selection);
                    
                    // 复制成功后切换为成功图标
                    if (finalSuccessIcon != null) {
                        copyButton.setIcon(finalSuccessIcon);
                        // 1.5秒后恢复为原始图标
                        Timer timer = new Timer(SUCCESS_ICON_DURATION, evt -> {
                            if (finalCopyIcon != null) {
                                copyButton.setIcon(finalCopyIcon);
                            }
                        });
                        timer.setRepeats(false);
                        timer.start();
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(
                        SwingUtilities.getWindowAncestor(copyButton),
                        "复制失败: " + ex.getMessage(),
                        "错误",
                        JOptionPane.ERROR_MESSAGE
                    );
                }
            }
        });
        
        return copyButton;
    }
    
    /**
     * 加载图标并调整大小
     * 
     * @param path 图标路径
     * @param width 目标宽度
     * @param height 目标高度
     * @return 调整大小后的图标,如果加载失败返回null
     */
    private static ImageIcon loadIcon(String path, int width, int height) {
        try {
            ImageIcon rawIcon = new ImageIcon(path);
            if (rawIcon.getIconWidth() <= 0 || rawIcon.getIconHeight() <= 0) {
                return null;
            }
            Image scaled = rawIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
            return new ImageIcon(scaled);
        } catch (Exception ex) {
            System.err.println("无法加载图标: " + path + " - " + ex.getMessage());
            return null;
        }
    }
    
    /**
     * 从组件树中查找 JTextArea 并获取其文本
     * 
     * @param components 组件数组
     * @return 找到的文本,如果未找到返回null
     */
    private static String findTextFromComponents(Component[] components) {
        for (Component comp : components) {
            if (comp instanceof JTextArea) {
                return ((JTextArea) comp).getText();
            } else if (comp instanceof Container) {
                String text = findTextFromComponents(((Container) comp).getComponents());
                if (text != null) {
                    return text;
                }
            }
        }
        return null;
    }
}