JTabbedPane 自定义显示效果
直接贴点代码吧package inci.emp.ctrl.whole;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Insets;import java.awt.LayoutManager;import java.awt.Window;import javax.swing.JComponent;import javax.swing.UIManager;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.basic.BasicTabbedPaneUI;public class RecTabbedPaneUI extends BasicTabbedPaneUI{/** * by: QQ: 725137 Name:whole */private Color defaultColor = new Color(192, 192, 192);private Color selectedColor = new Color(155, 159, 232);public static ComponentUI createUI(JComponent c){return new RecTabbedPaneUI();}protected void paintTabBackground(Graphics g, int tabPlacement,int tabIndex, int x, int y, int w, int h, boolean isSelected){g.setColor(!isSelected || selectedColor == null ? defaultColor: selectedColor);switch (tabPlacement){case LEFT:g.fillRect(x + 1, y + 1, w - 1, h - 3);break;case RIGHT:g.fillRect(x, y + 1, w - 2, h - 3);break;case BOTTOM:g.fillRect(x + 1, y, w - 3, h - 1);break;case TOP:default:g.fillRect(x + 1, y + 1, w - 3, h - 1);}}protected LayoutManager createLayoutManager(){return new TabbedPaneLayout();}// 设置Layoutpublic class TabbedPaneLayout extends BasicTabbedPaneUI.TabbedPaneLayout{protected void calculateTabRects(int tabPlacement, int tabCount){super.calculateTabRects(tabPlacement, tabCount);setRec(10);// 设置间距tabInsets.bottom = 10;//设置选项卡高度}public void setRec(int rec){for (int i = 0; i < rects.length; i++){rects.x = rects.x + rec * i;}}}public void setDefaultColor(Color defaultColor){this.defaultColor = defaultColor;}public void setSelectedColor(Color selectedColor){this.selectedColor = selectedColor;}protected void paintContentBorder(Graphics g, int tabPlacement,int 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);w -= (x - insets.left);break;case RIGHT:w -= calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);w += tabAreaInsets.left;break;case BOTTOM:h -= calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);h += tabAreaInsets.top;break;case TOP:default:y += calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);y -= tabAreaInsets.bottom;h -= (y - insets.top);}// Fill region behind content areaColor color = UIManager.getColor("TabbedPane.contentAreaColor");if (color != null){g.setColor(color);}else if (selectedColor == null){g.setColor(tabPane.getBackground());}else{g.setColor(selectedColor);}g.fillRect(x, y, w, h); g.setColor(selectedColor); Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(10.0f));g.drawLine(x+6, y+5,w, y+5); g2d.setStroke(new BasicStroke(1.0f)); }}
页:
[1]