Java JTabbedPane:选项卡左侧的图标

Java JTabbedPane:选项卡左侧的图标,java,swing,icons,jtabbedpane,nimbus,Java,Swing,Icons,Jtabbedpane,Nimbus,您好,我正在使用nimbus外观,有一个带有图标和文本的选项卡窗格。 现在图标出现在文本的右侧,而我希望它出现在左侧 我还想在图标和文本之间添加一些间距 谢谢 您需要自己设置选项卡组件;它控制选项卡标题的呈现方式 // Create tabbed pane and add tabs. JTabbedPane tabbedPane = ... // Create bespoke component for rendering the tab. JLabel lbl = new JLabel("H

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

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


谢谢

您需要自己设置选项卡组件;它控制选项卡标题的呈现方式

// 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);
}

我使用这段代码将(组件、字符串、图标和工具提示)添加到选项卡窗格中;现在我想要相同的(文本显示在右边,图标显示在左边)我尝试了上面你评论中指定的相同的u…我的代码中出现了异常…请help@Gagan93:请将此作为单独的问题发布;没有看到例外,我不确定我能帮上忙。