Java JTabbedPane将标题文本对齐到中心

时间:2014-02-19 02:37:52

标签: java swing text jtabbedpane

我正在创建自己的自定义JTabbedPane对象,其中包含一个扩展JTabbedPane的简单小类。我已经完成了相当多的工作,但最近发现了一个问题,即标签的标题文本不会自动对齐标签的中心。我想要它。这是它的样子:

Just imagine that the tabs aren't aligned to the center

正如我所说,它只是JTabbedArea的扩展,如下所示:

private static final long serialVersionUID = 1L;
private String name;
private final int width = 150, height = 50;
public ColoredTabs(String paneName,  int tabPlacement, String[] names, Color[] colors, JComponent[] components){
    super(tabPlacement);
    this.name = paneName;
    if(names.length != components.length || names.length != colors.length || components.length != colors.length){
        throw new IllegalArgumentException("The arguments for COMPONENTS, COLORS, and NAMES do not match up for '"+this.name+"'...");
    }
    setFont(Resources.getFont());
    setBackground(Color.YELLOW);
    for(int i = 0; i < names.length; i++){
        addTab(names[i], components[i]);
        setBackgroundAt(i, colors[i]);
        setIconAt(i, new ImageIcon(new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)));

    }
}

我经历制作BufferedImage的麻烦的原因是为了拉伸Tab的区域,对于ICON,也许有一种方法可以将文本置于BufferedImage的中心,任何想法?

1 个答案:

答案 0 :(得分:3)

对于自定义tabbedPane,您应该能够创建具有设置对齐的标签,然后将它们添加到jTabbedPane,如下所示:

//Create new label to be used as a tab name
JLabel tabLabel = new JLabel("Tab", JLabel.CENTER);
//add new label at set location
jTabbedPane.setTabComponentAt(0, tabLabel);

如需更多帮助,请告诉我们您如何创建标签。

编辑: 这可能是相关的: Aligning icon to the left in JTabbedPane in Nimbus Look and Feel