JTabbedPane自定义选项卡外观 [英] JTabbedPane customize tab look

查看:161
本文介绍了JTabbedPane自定义选项卡外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JTabbedPane中自定义选项卡的外观。

我想从最简单和最简洁的行为开始:没有边框,纯色。

问题是不稳定仍然存在:标签略有边缘重叠。

I want to customize the look of the tabs in JTabbedPane.
I want to start from the simplest and plainest behavior: no borders, solid color.
The problem is that a non-plainess still remains: the tabs slight margin overlap.

您会看到,由于选择了第二个标签,因此它会脱颖而出。
这是通过略微的保证金重叠来实现的。
是否有(非常棘手的)方法来禁用此行为?

You see that since the second tab is selected, it is "brought to the fore". This is achieved by a slight margin overlap. Is there a (non tricky) way to disable this behavior?

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

simple, testable (just fix imports) code:

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

}

推荐答案

正确的方法是实施 Custom外观和感觉只有。但是如果你想玩 XxxTabbedPaneUI ,那么可能这篇文章可以帮助你。

Correct way would be to implement Custom Look & Feel only. But if you want to play with XxxTabbedPaneUI, then maybe this post can help you with that.

Nimbus 最好检查 aephyr 代码库

这篇关于JTabbedPane自定义选项卡外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆