Swing JTabbedPane如何设置滚动宽度?

时间:2012-05-10 17:10:00

标签: java swing user-interface jtabbedpane

我使用以下代码创建JTabbedPane

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

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

enter image description here

如何使卷轴宽度更宽​​,以使其适合选项卡选择区域?

1 个答案:

答案 0 :(得分: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);
            }
        };
    }
}

设置新的用户界面:

tabbedPane.setUI(new ExtendedTabbedPaneUI());