java - JTabbedPane:选项卡左侧的图标

标签 java swing icons jtabbedpane nimbus

你好,我正在使用灵气外观,并且有一个带有图标和文本的选项卡式 Pane 。 现在图标出现在文本的右侧,而我希望它出现在左侧。

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

谢谢!

最佳答案

需要自己设置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);
}

关于java - JTabbedPane:选项卡左侧的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1782224/

相关文章:

java - 如何在 Java 中对自定义 ArrayList 进行排序?

html - html和css中的图标栏自定义

java - TableCellEditor 带有一个不更新值的对话框

java - 如何以一秒为间隔显示 Swing 计时器的剩余时间?

Java Swing 定时器

iphone - 如何删除应用程序图标上的 "white reflection"?

icons - 图标上的 Google Chrome 扩展程序号码

java - 使用数组的内容打印字符串

java - 为什么 Optional 的 or 和 flatMap 方法的供应商类型参数是通配符?

java - 如何为 java spring maven 应用程序映射域?