张世豪
2025-12-11 6f59464fc6e12a525ba3004864eceb1bc1573a31
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
package zhuye;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
 
/**
 * 提供统一按钮样式的工厂方法。
 */
public final class buttonset {
    private static final Dimension DEFAULT_SIZE = new Dimension(90, 36);
 
    private buttonset() {
        // 工具类不需要实例化
    }
 
    /**
     * 创建带有统一样式的按钮,供多个界面复用。
     *
     * @param text 按钮显示文本
     * @param backgroundColor 按钮背景色
     * @return 已应用样式的 JButton
     */
    public static JButton createStyledButton(String text, Color backgroundColor) {
        final Color baseColor = backgroundColor != null ? backgroundColor : new Color(70, 130, 180);
 
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.BOLD, 14));
        button.setBackground(baseColor);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createEmptyBorder(8, 18, 8, 18));
 
    Dimension preferred = button.getPreferredSize();
    int width = Math.max(preferred.width, DEFAULT_SIZE.width);
    int height = Math.max(preferred.height, DEFAULT_SIZE.height);
    Dimension adjustedSize = new Dimension(width, height);
 
    button.setPreferredSize(adjustedSize);
    button.setMinimumSize(new Dimension(DEFAULT_SIZE.width, DEFAULT_SIZE.height));
    button.setMaximumSize(adjustedSize);
 
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseEntered(java.awt.event.MouseEvent event) {
                button.setBackground(brightenColor(baseColor));
            }
 
            @Override
            public void mouseExited(java.awt.event.MouseEvent event) {
                button.setBackground(baseColor);
            }
        });
 
        return button;
    }
 
    private static Color brightenColor(Color color) {
        if (color == null) {
            return new Color(200, 200, 200);
        }
        int r = Math.min(255, color.getRed() + 30);
        int g = Math.min(255, color.getGreen() + 30);
        int b = Math.min(255, color.getBlue() + 30);
        return new Color(r, g, b);
    }
}