java JTabbedPane:选项卡左侧的图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1782224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JTabbedPane: icon on left side of tabs
提问by clamp
hello i am using the nimbus look-and-feel and have a tabbedpane with an icon and text. now the icon appears on the right side of the text, while i would like to have it on the left side.
你好,我正在使用 nimbus 外观和感觉,并有一个带有图标和文本的选项卡面板。现在图标出现在文本的右侧,而我希望它在左侧。
also i would like to add some spacing between the icon and the text.
我还想在图标和文本之间添加一些间距。
thanks!
谢谢!
回答by Adamski
You need to set the tab component yourself; which governs how the tab title is rendered.
需要自己设置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);
Obviously you could encapsulate this in a utility method:
显然,您可以将其封装在实用程序方法中:
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);
}

