JTabbedPane自定义选项卡外观

Ago*_*noX 8 java swing jtabbedpane

我想在JTabbedPane中自定义选项卡的外观.
我想从最简单和最简洁的行为开始:没有边界,纯色.
问题是仍然存在不稳定性:标签略微重叠.

在此输入图像描述

您会看到,自从选择了第二个选项卡后,它就会"脱颖而出".这是通过略微的边缘重叠来实现的.是否有(非常棘手的)方法来禁用此行为?

简单,可测试(只修复导入)代码:

public class TabbedPane_LookStudy extends JFrame{

public static void main(String [] args) throws UnsupportedLookAndFeelException {
    UIManager.setLookAndFeel(new NimbusLookAndFeel());
    new TabbedPane_LookStudy().setVisible(true);
}

public TabbedPane_LookStudy() {
    JTabbedPane tp = new JTabbedPane();
    tp.setUI(new MyTabbedPaneUI());
    add(tp);

    tp.addTab("first",new JPanel());
    tp.addTab("second", new JPanel());
    tp.addTab("third", new JPanel());

    setPreferredSize(new Dimension(180,100));
    pack();
}

public static class MyTabbedPaneUI extends javax.swing.plaf.basic.BasicTabbedPaneUI {

    @Override
    protected void paintTab(Graphics g, int tabPlacement, Rectangle[] rects, 
               int tabIndex, Rectangle iconRect, Rectangle textRect) {
        Color savedColor = g.getColor();
        g.setColor(Color.PINK);
        g.fillRect(rects[tabIndex].x, rects[tabIndex].y, 
               rects[tabIndex].width, rects[tabIndex].height);
        g.setColor(Color.BLUE);
        g.drawRect(rects[tabIndex].x, rects[tabIndex].y, 
               rects[tabIndex].width, rects[tabIndex].height);
        g.setColor(savedColor);
    }
 }
Run Code Online (Sandbox Code Playgroud)

}

mKo*_*bel 4

正确的方法是仅实现自定义外观和感觉。但如果你想玩XxxTabbedPaneUI,那么这篇文章也许可以帮助你。

对于Nimbus最好检查aephyr代码库