zouyu
2025-11-26 9552ea3618897507bc00910e170019ac8867423b
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
package com.xindao.ocr.swingui.swing.utils;
 
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
 
/**
 * 生成自定义主键工具类
 */
public class GenerateCustomizeComponent {
 
    private static final Color PRIMARY_LIGHT = new Color(100, 150, 255);
    private static final Color PRIMARY_COLOR = new Color(66, 133, 244);
    private static final Color TEXT_COLOR = new Color(51, 51, 51);
 
    private static final Color SECONDARY_COLOR = new Color(76, 175, 80);
    private static final Color CARD_COLOR = new Color(255, 255, 255);
 
    // 创建卡片式面板(带阴影和圆角)
    public static JPanel createCardPanel() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 
                // 绘制背景
                g2d.setColor(CARD_COLOR);
                g2d.fill(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 10, 10));
 
                // 绘制阴影
                g2d.setColor(new Color(0, 0, 0, 10));
                for (int i = 0; i < 3; i++) {
                    g2d.draw(new RoundRectangle2D.Double(i, i, getWidth() - 2*i, getHeight() - 2*i, 10, 10));
                }
 
                g2d.dispose();
            }
        };
        panel.setOpaque(false);
        panel.setBackground(CARD_COLOR);
        return panel;
    }
 
 
    // 创建样式化面板
    public static JPanel createStyledPanel(LayoutManager layout) {
        JPanel panel = new JPanel(layout);
        panel.setOpaque(false);
        return panel;
    }
 
    // 创建主要按钮(强调色)
    public static JButton createPrimaryButton(String text,Font DEFAULT_FONT) {
        JButton button = new JButton(text) {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 
                // 渐变背景
                GradientPaint gradient;
                if (getModel().isPressed()) {
                    gradient = new GradientPaint(0, 0, PRIMARY_COLOR.darker(), 0, getHeight(), PRIMARY_COLOR.darker().darker());
                } else if (getModel().isRollover()) {
                    gradient = new GradientPaint(0, 0, PRIMARY_LIGHT, 0, getHeight(), PRIMARY_COLOR);
                } else {
                    gradient = new GradientPaint(0, 0, PRIMARY_COLOR, 0, getHeight(), PRIMARY_LIGHT);
                }
 
                g2d.setPaint(gradient);
                g2d.fill(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 6, 6));
                super.paintComponent(g);
                g2d.dispose();
            }
        };
 
        button.setFont(new Font(DEFAULT_FONT.getName(), Font.BOLD, 14));
        button.setForeground(Color.WHITE);
        button.setBorder(new EmptyBorder(8, 15, 8, 15));
        button.setContentAreaFilled(false);
        button.setFocusPainted(false);
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 
        return button;
    }
 
    // 创建样式化按钮
    public static JButton createStyledButton(String text,Font font) {
        JButton button = new JButton(text) {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 
                if (getModel().isPressed()) {
                    g2d.setColor(new Color(230, 230, 230));
                } else if (getModel().isRollover()) {
                    g2d.setColor(new Color(240, 240, 240));
                } else {
                    g2d.setColor(new Color(235, 235, 235));
                }
 
                g2d.fill(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 6, 6));
                super.paintComponent(g);
                g2d.dispose();
            }
        };
 
        button.setFont(font);
        button.setForeground(TEXT_COLOR);
        button.setBorder(new EmptyBorder(8, 15, 8, 15));
        button.setContentAreaFilled(false);
        button.setFocusPainted(false);
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 
        return button;
    }
 
}