1

我使用以下代码创建一个 JTabbedPane

new JTabbedPane(JTabbedPane.LEFT,JTabbedPane.SCROLL_TAB_LAYOUT);

它导致滚动宽度小于选项卡选择区域

在此处输入图像描述

如何使滚动宽度更宽以适合选项卡选择区域?

4

1 回答 1

5

您可以扩展BasicTabbedPaneUI和实现您自己的按钮以createScrollButton()提供新的首选尺寸。看起来BasicTabbedPaneUI这些按钮有自己的私有实现 - ScrollableTabButton. 您可以创建类似的东西,如下所示:

public class ExtendedTabbedPaneUI extends BasicTabbedPaneUI {

    @Override
    protected JButton createScrollButton(int direction) {
         if (direction != SOUTH && direction != NORTH && direction != EAST &&
                                   direction != WEST) {
             throw new IllegalArgumentException("Direction must be one of: " +
                                                "SOUTH, NORTH, EAST or WEST");
         }

         //return new ScrollableTabButton(direction);

         return new BasicArrowButton(direction,
            UIManager.getColor("TabbedPane.selected"),
            UIManager.getColor("TabbedPane.shadow"),
            UIManager.getColor("TabbedPane.darkShadow"),
            UIManager.getColor("TabbedPane.highlight")) {

            @Override
            public Dimension getPreferredSize() {
                int maxWidth = calculateMaxTabWidth(JTabbedPane.LEFT);
                return new Dimension(maxWidth, super.getPreferredSize().height);
            }
        };
    }
}

并设置新的 UI:

tabbedPane.setUI(new ExtendedTabbedPaneUI());
于 2012-05-10T18:25:12.183 回答