DELL
2025-04-28 e6eb7fb0af366e370f125668d62e89eb0004f517
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
package main;
 
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
 
 
public class MyTabbedPaneUI extends BasicTabbedPaneUI {
 
    // ²»ÖªµÀÊǸöʲô¶«Î÷£¬»æÖÆpaneʱ£¬¼ÆËã×ø±êºÍ¿í¸ßµÄ·½·¨ÐèÒªÓõ½£¬Ö±½Ó´Ó¸¸À࿽±´¹ýÀ´µÄ
    private boolean tabsOverlapBorder = UIManager.getBoolean("TabbedPane.tabsOverlapBorder");
 
    // ±ß¿òºÍ±³¾°µÄÑÕÉ«
    private Color SELECT_COLOR = new Color(57, 181, 215);
 
    /*// »æÖÆÕû¸öÑ¡Ïî¿¨ÇøÓò
    @Override
    protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
        // Èç¹ûûÓÐÌØÊâÒªÇ󣬾ÍʹÓÃĬÈϵĻæÖÆ·½°¸
        super.paintTabArea(g, tabPlacement, selectedIndex);
    }*/
 
    // »æÖÆtabҳǩµÄ±ß¿ò
    @Override
    protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
        // ²âÊÔÁËһϣ¬·¢ÏÖûÓб߿ò¸üºÃ¿´Ò»µã¶ù
        /*g.setColor(SELECT_COLOR);
        switch (tabPlacement) {
            case LEFT:
                g.drawLine(x, y, x, y + h - 1);
                g.drawLine(x, y, x + w - 1, y);
                g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
                break;
            case RIGHT:
                g.drawLine(x, y, x + w - 1, y);
                g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
                g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
                break;
            case BOTTOM:
                g.drawLine(x, y, x, y + h - 1);
                g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
                g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
                break;
            case TOP:
            default:
                g.drawLine(x, y, x, y + h - 1);
                g.drawLine(x, y, x + w - 1, y);
                g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
        }*/
    }
 
    // »æÖÆÑ¡ÖеÄÑ¡Ï±³¾°É«
    @Override
    protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
        Graphics2D g2d = (Graphics2D) g;
        GradientPaint gradient;
        switch (tabPlacement) {
            case LEFT:
                if (isSelected) {
                    gradient = new GradientPaint(x + 1, y, SELECT_COLOR, x + w, y, Color.WHITE, true);
                } else {
                    gradient = new GradientPaint(x + 1, y, Color.LIGHT_GRAY, x + w, y, Color.WHITE, true);
                }
                g2d.setPaint(gradient);
                g.fillRect(x + 1, y + 1, w - 1, h - 2);
                break;
            case RIGHT:
                if (isSelected) {
                    gradient = new GradientPaint(x + w, y, SELECT_COLOR, x + 1, y, Color.WHITE, true);
                } else {
                    gradient = new GradientPaint(x + w, y, Color.LIGHT_GRAY, x + 1, y, Color.WHITE, true);
                }
                g2d.setPaint(gradient);
                g.fillRect(x, y + 1, w - 1, h - 2);
                break;
            case BOTTOM:
                if (isSelected) {
                    gradient = new GradientPaint(x + 1, y + h, SELECT_COLOR, x + 1, y, Color.WHITE, true);
                } else {
                    gradient = new GradientPaint(x + 1, y + h, Color.LIGHT_GRAY, x + 1, y, Color.WHITE, true);
                }
                g2d.setPaint(gradient);
                g.fillRect(x + 1, y, w - 2, h - 1);
                break;
            case TOP:
            default:
                if (isSelected) {
                    gradient = new GradientPaint(x + 1, y, SELECT_COLOR, x + 1, y + h, Color.WHITE, true);
                } else {
                    gradient = new GradientPaint(x + 1, y, Color.LIGHT_GRAY, x + 1, y + h, Color.WHITE, true);
                }
                g2d.setPaint(gradient);
                g2d.fillRect(x + 1, y + 1, w - 2, h - 1);
        }
 
    }
 
    // »æÖÆTabbedPaneÈÝÆ÷µÄËÄÖܱ߿òÑùʽ
    @Override
    protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
        // Èç¹û²»ÏëÒª±ß¿ò£¬Ö±½ÓÖØÐ´Ò»¸ö¿Õ·½·¨
        // super.paintContentBorder(g, tabPlacement, selectedIndex);
 
        // ÕâЩ¼ÆËã×ø±êºÍ¿í¸ßµÄ´úÂ룬ֱ½Ó´Ó¸¸À࿽±´³öÀ´ÖØÓü´¿É
        int width = tabPane.getWidth();
        int height = tabPane.getHeight();
        Insets insets = tabPane.getInsets();
        Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
 
        int x = insets.left;
        int y = insets.top;
        int w = width - insets.right - insets.left;
        int h = height - insets.top - insets.bottom;
 
        switch (tabPlacement) {
            case LEFT:
                x += calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
                if (tabsOverlapBorder) {
                    x -= tabAreaInsets.right;
                }
                w -= (x - insets.left);
                break;
            case RIGHT:
                w -= calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
                if (tabsOverlapBorder) {
                    w += tabAreaInsets.left;
                }
                break;
            case BOTTOM:
                h -= calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
                if (tabsOverlapBorder) {
                    h += tabAreaInsets.top;
                }
                break;
            case TOP:
            default:
                y += calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
                if (tabsOverlapBorder) {
                    y -= tabAreaInsets.bottom;
                }
                h -= (y - insets.top);
        }
 
        // Ëĸö±ß¿òµÄ»æÖÆ·½·¨£¬¶¼×Ô¼ºÖØÐ´Ò»±é£¬·½±ã¿ØÖÆÑÕÉ«ºÍÒ»Ð©ÌØÐ§
        paintContentBorderTopEdge(g, tabPlacement, selectedIndex, x, y, w, h);
        paintContentBorderLeftEdge(g, tabPlacement, selectedIndex, x, y, w, h);
        paintContentBorderBottomEdge(g, tabPlacement, selectedIndex, x, y, w, h);
        paintContentBorderRightEdge(g, tabPlacement, selectedIndex, x, y, w, h);
    }
 
    // »æÖÆÈÝÆ÷×ó²à±ß¿ò£¬²»ÊÇtab£¬ÊÇpane
    // ÉÏÏÂ×óÓÒ£¬¶¼¿ÉÒÔÖØÐ´·½·¨À´»æÖÆ£¬ÏàÓ¦µÄ·½·¨£ºpaintContentBorder*Edge()£¬ÓÉpaintContentBorder()·½·¨Í³Ò»µ÷ÓÃ
    @Override
    protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
        g.setColor(SELECT_COLOR);
        g.drawLine(x, y, x, y + h - 2);
    }
 
    @Override
    protected void paintContentBorderTopEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
        g.setColor(SELECT_COLOR);
        g.drawLine(x, y, x + w - 2, y);
    }
 
    // ÓұߺÍϱߣ¬ÕâÁ½¸öÐèҪעÒ⣬x + w ºÍ y + h ÒÑ´ïµ½±ß¿òÁٽ磬±ØÐë¼õµô¼¸¸öÊýÖµ£¬·ñÔò±ß¿ò»áÏÔʾ²»³öÀ´
    @Override
    protected void paintContentBorderRightEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
        g.setColor(SELECT_COLOR);
        g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
    }
 
    @Override
    protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
        g.setColor(SELECT_COLOR);
        g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
    }
 
    // »æÖÆÑ¡ÖÐij¸öTabºó£¬»ñµÃ½¹µãµÄÑùʽ
    @Override
    protected void paintFocusIndicator(Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect, boolean isSelected) {
        // ÖØÐ´¿Õ·½·¨£¬Ö÷ÒªÓÃÀ´È¥µôÐéÏß
    }
 
    // ¼ÆËãtabҳǩµÄ¿í¶È
    @Override
    protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
        // ¿É¸ù¾ÝplacementÀ´×ö²»Í¬µÄµ÷Õû
        return super.calculateTabWidth(tabPlacement, tabIndex, metrics) + 20;
    }
 
    // ¼ÆËãtabҳǩµÄ¸ß¶È
    @Override
    protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
        // ¿É¸ù¾ÝplacementÀ´×ö²»Í¬µÄµ÷Õû
        return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight) + 20;
    }
 
}