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 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; } }