java - Swing JTabbedPane如何设置滚动宽度?

标签 java swing user-interface jtabbedpane

我使用以下代码创建 JTabbedPane

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

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

enter image description here

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

最佳答案

您可以扩展BasicTabbedPaneUI并在createScrollButton()中实现您自己的按钮,提供新的首选尺寸。看起来 BasicTabbedPaneUI 对这些按钮有自己的私有(private)实现 - 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());

关于java - Swing JTabbedPane如何设置滚动宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10539013/

相关文章:

java - 有没有办法在Java中的抽象接口(interface)方法中要求特定值作为参数?

java - 使 Eclipse 表现得像 Visual Studio

java - 如何将 JLabel 添加到另一个类的 JFrame 中?

user-interface - 在 Tkinter 中使用 pygame 功能

android - 这个气球状的 UI 组件叫什么名字? (安卓/Linux)

java - 无法编译此命令(Java 和 Ant 相关)

java - 使用netbeans在java中更改密码程序

java - 从 JList 中删除项目

c++ - 使用文本调整 QToolButton 的大小

java - WebView 和前置摄像头在某些三星设备上无法使用