【问题标题】:how to set font for a tab title in jtabbedpane如何在 jtabbedpane 中设置选项卡标题的字体
【发布时间】:2017-12-15 01:02:06
【问题描述】:

我设计了一个包含一些标签的tabbedpane,有没有办法为标签的标题设置字体? 我读过this useful question 但是通过这种方法我们只能设置边框的标题而不是选项卡的标题。

一个简单的代码来解释我的问题:

private  JTabbedPane tabbedPane;
private  JPanel tab1;
private  Jpanel tab2;
...
tabbedPane.insert("title1", icon1, tab1, "tip1", 0);
tabbedPane.insert("title2", icon2, tab2, "tip2", 1);

如何更改“title1”或“title2”的字体?


编辑:

根据亲爱的Jay Smith的回答,我意识到这种方法更强大,但是我还有一些其他问题,请看这张图:

我为每个选项卡创建一个 JPanel(即称为 myPanel),其中包含两个 JLabel,一个用于 TabIcon,另一个用于 TabTitle。最后,我写了这个:

tabbedPane.setTabComponentAt(0, myPanel);

现在,有什么问题:

  1. 首先,请注意我使用的波斯语是 从右到左,所以我想把图标拉到最左边的位置 tabTitle 到最右边的位置。
  2. 其次,根据第一个选项卡的边框,我想要那个面板填充 tabHeader 的整个空间。

【问题讨论】:

  • UIManager.getDefaults().getFont("TabbedPane.font") 是一种蛮力方法,如果您需要更多自定义/控制,那么您需要提供自己的自定义标题组件

标签: java swing fonts


【解决方案1】:

您可以将任何 Swing Component 设置为 JTabbedPane 中的选项卡。

JLabel 作为标签的示例:

JLabel label = new JLabel("Tab 1");
label.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 24));

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabComponentAt(0, label);

【讨论】:

  • 您的方法优雅且功能强大,但我在对齐、方向和填充父组件方面遇到了一些新问题。请查看我编辑的问题。
  • @Tot Zam 请修改问题的编辑部分。
【解决方案2】:

我的朋友,这太容易了。执行以下操作即可完成。

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setFont( new Font( "Dialog", Font.BOLD|Font.ITALIC, 24 ) );

【讨论】:

  • 快乐编码 :)
猜你喜欢
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 2012-08-24
  • 2014-12-06
  • 1970-01-01
相关资源
最近更新 更多