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