From b518f895dec5264fd25e22a68300c40ceba6f43d Mon Sep 17 00:00:00 2001
From: 826220679@qq.com <826220679@qq.com>
Date: 星期六, 20 十二月 2025 15:30:20 +0800
Subject: [PATCH] 新增了按钮功能
---
src/zhuye/Fuzhibutton.java | 270 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 270 insertions(+), 0 deletions(-)
diff --git a/src/zhuye/Fuzhibutton.java b/src/zhuye/Fuzhibutton.java
new file mode 100644
index 0000000..340977f
--- /dev/null
+++ b/src/zhuye/Fuzhibutton.java
@@ -0,0 +1,270 @@
+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 璋冩暣澶у皬鍚庣殑鍥炬爣锛屽鏋滃姞杞藉け璐ヨ繑鍥瀗ull
+ */
+ 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 鎵惧埌鐨勬枃鏈紝濡傛灉鏈壘鍒拌繑鍥瀗ull
+ */
+ 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;
+ }
+}
+
--
Gitblit v1.10.0