本文介绍了JTabbedPane:标签左侧的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我正在使用灵气的外观,并有一个带有图标和文字的tabbedpane。
现在图标显示在文本的右侧,而我想在左侧显示。
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.
我还想添加一些图标和文字之间的间距。
also i would like to add some spacing between the icon and the text.
谢谢!
推荐答案
您需要自己设置标签组件;它控制选项卡标题的呈现方式。
You need to set the tab component yourself; which governs how the tab title is rendered.
// 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);
}
这篇关于JTabbedPane:标签左侧的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!