【问题标题】:JTabbedPane: icon on left side of tabsJTabbedPane:选项卡左侧的图标
【发布时间】:2010-12-19 10:15:50
【问题描述】:

您好,我正在使用 nimbus 外观,并有一个带有图标和文本的选项卡窗格。 现在图标出现在文本的右侧,而我希望它出现在左侧。

我还想在图标和文本之间添加一些间距。

谢谢!

【问题讨论】:

    标签: java swing icons jtabbedpane nimbus


    【解决方案1】:

    需要自己设置tab组件;它控制标签标题的呈现方式。

    // Create tabbed pane and add tabs.
    JTabbedPane tabbedPane = ...
    
    // Create bespoke component for rendering the tab.
    JLabel lbl = new JLabel("Hello, World");
    Icon icon = new ImageIcon(getClass().getResource("/foo/bar/hello.jpg"));
    lbl.setIcon(icon);
    
    // Add some spacing between text and icon, and position text to the RHS.
    lbl.setIconTextGap(5);
    lbl.setHorizontalTextPosition(SwingConstants.RIGHT);
    
    // Assign bespoke tab component for first tab.
    tabbedPane.setTabComponentAt(0, lbl);
    

    显然你可以将它封装在一个实用方法中:

    private void addTab(JTabbedPane tabbedPane, Component tab, String title, Icon icon) {
      tabbedPane.add(tab);
    
      JLabel lbl = ... // Create bespoke label for rendering tab title.
    
      tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, lbl);
    }
    

    【讨论】:

    • 我正在使用此代码将(组件、字符串、图标和工具提示)添加到选项卡 pane.addTab("Name of Tab", new ImageIcon("resources\\1.png"),主面板,“工具提示文本”);现在我想要相同的(文本出现在右边,图标出现在左边)我尝试了上面评论中指定的相同的你......我的代码中有异常......请帮助
    • @Gagan93:请您将其作为一个单独的问题发布;没有看到异常我不确定我能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多