2014-02-19 56 views
3

我創建了我自己的自定義JTabbedPane對象,其中包含一個簡單的小類,用於擴展JTabbedPane。我已經完成了相當多的工作,但最近發現了一個問題,即選項卡的標題文本不會自動對齊到選項卡的中心。我希望它。這裏是什麼樣子:Java 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的中心,有什麼想法?

回答

3

對於自定義選項卡窗格,你應該能夠用一組排列創建標籤,然後將它們添加到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

+0

是啊,你說得對。最好的方法是將JLabel放入該區域,現在我只是使用JLabel來拉伸Tab的區域,謝謝。 – tanishalfelven