JTabbedPane 选项卡组件调整大小以适应 [英] JTabbedPane tab component resize to fit

查看:44
本文介绍了JTabbedPane 选项卡组件调整大小以适应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的标签标签共享 JTabbedPane 宽度.如果有一个标签,适合整个宽度,如果两个标签,共享宽度,如果三个,每个 1/3 等等......

I want to make my tab labels share the JTabbedPane width. If there's one tab, fit whole width, if two tabs, share width, if three, 1/3 for each and so on...

我什至不知道是否可以在不放置组件并调整其大小的情况下完成此操作,也许 JTabbedPane 有一种通过方法调整其选项卡标签大小的方法,但我不知道...

i don't even know if it's possible to do it without putting a component there and resizing it, maybe JTabbedPane has a way of resizing it's tab label via method and i don't know...

有人知道如何以最简单的方式制作它吗?

Anyone got any idea how to make it by the easiest way possible?

推荐答案

正如@trashgod 已经指出的,选项卡布局由 LAF 特定的 SomeLAFTabbedPaneUI 处理,更具体地说是 TabbedPaneLayout.所以要走的路是

As @trashgod already noted, the tab layout is handled by the LAF-specific SomeLAFTabbedPaneUI, more specifically the TabbedPaneLayout. So the way to go is

  • 实现一个自定义的子类 MySomeLAFTabbedPaneUI,它有一个自定义的扩展 TabbedPaneLayout(你需要为你想要支持的每个 SomeLAF 执行此操作
  • 用您的自定义类替换普通用户界面

第一个归结为挂钩用于绘制选项卡/定位自定义 tabComponents 的矩形的计算.类似于(注意:显然还没有准备好生产:-)

The first boils down to hook into the calculation of the rectangles which are used for painting the tabs/locating custom tabComponents. Something like (note: obviously not production ready :-)

public class XMetalTabbedPaneUI extends MetalTabbedPaneUI {

    public static ComponentUI createUI(JComponent c) {
        return new XMetalTabbedPaneUI();
    }

    @Override
    protected LayoutManager createLayoutManager() {
        return new XTabbedPaneLayout();
    }


    protected class XTabbedPaneLayout extends MetalTabbedPaneUI.TabbedPaneLayout {

        protected Container tabContainer;

        @Override
        protected void calculateTabRects(int tabPlacement, int tabCount) {
            super.calculateTabRects(tabPlacement, tabCount);
            // TODO: check if it makes sense to stretch
            int max = 0;
            int sum = 0;
            Rectangle r = new Rectangle();
            for (int i = 0; i < tabCount; i++) {
                getTabBounds(i, r);
                max = Math.max(max, r.width);
                sum += r.width;
            }
            // TODO: calculate real width, that is -insets
            int paneWidth = tabPane.getWidth() - 10; 
            int free = paneWidth - sum;
            // nothing to distribute
            if (free < tabCount) return;
            int add = free /tabCount;
            int offset = 0;
            for (int i = 0; i < tabCount; i++) {
                r = rects[i]; 
                r.x += offset;
                r.width += add;
                offset += add;
            }

        }

    }
}

第二个被 SwingX (实际上,您只需要它的 plaf 模块和依赖项).它的基本构建块是一个 TabbedPaneAddon,用于加载自定义 ui:

The second is highly simplified (biased me, as the maintainer of the project :-) by the plaf enhancement mechanism provided by SwingX (actually all you need is its plaf module and dependencies). Its basic building block is a TabbedPaneAddon which loads the custom ui:

public class TabbedPaneAddon extends AbstractComponentAddon {

    /**
     * @param name
     */
    public TabbedPaneAddon() {
        super("TabbedPane");
    }

    @Override
    protected void addMetalDefaults(LookAndFeelAddons addon,
            DefaultsList defaults) {
        // remove old ui
        UIManager.getLookAndFeelDefaults().put("TabbedPaneUI", null);
        defaults.add("TabbedPaneUI",
                // here goes the full classname of your custom ui
                // this is an example only :-)
                "org.jdesktop.swingx.XMetalTabbedPaneUI");
    }

    // implement other addXXDefault as needed for 
    // supporting more LAFs
}

要进行替换,您必须在您的应用程序中贡献插件(早期"):

The make the replacement happen, you have to contribute the addon ("early") in your application:

LookAndFeelAddons.contribute(new TabbedPaneAddon()); 

这篇关于JTabbedPane 选项卡组件调整大小以适应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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