2013-08-30 98 views
0

我爲我的JTabbedPane創建了一個自定義LaF,唯一的問題是它只顯示標籤,而不是文本,請幫助,我已經嘗試了一切!自定義JTabbedPane LaF不顯示文本

package com.jama.lyricist.ui; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.Insets; 
import java.awt.Rectangle; 

import javax.swing.plaf.basic.BasicTabbedPaneUI; 

public class AdvancedTabbedPaneUI extends BasicTabbedPaneUI{ 

    @Override 
    protected void paintTab(Graphics g, int tabPlacement, Rectangle[] rects, int  tabIndex, Rectangle iconRect, Rectangle textRect) { 
     Color savedColor = g.getColor(); 
     g.setColor(new Color(238,238,238)); 
     g.fillRect(rects[tabIndex].x, rects[tabIndex].y, 
       rects[tabIndex].width, rects[tabIndex].height); 
     g.setColor(new Color(153, 153, 153)); 
     g.drawRect(rects[tabIndex].x, rects[tabIndex].y, 
       rects[tabIndex].width, rects[tabIndex].height); 
     g.setColor(new Color(102,102,102)); 
     g.setColor(savedColor); 
    } 


    private final Insets borderInsets = new Insets(0, 0, 0, 0); 
    @Override 
    protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) { 
    } 
    @Override 
    protected Insets getContentBorderInsets(int tabPlacement) { 
     return borderInsets; 
    } 

    @Override 
    protected void paintContentBorderTopEdge(Graphics g, int tabPlacement, 
          int selectedIndex, int x, int y, int w, int h) { 
     super.paintContentBorderTopEdge(g, tabPlacement, -1, x, y, w, h); 
    } 

}

它輸出這樣的:Output

+2

你在哪兒畫標籤標題?我想你需要在'paintTab'方法的某個地方拋出'paintText',如[這裏]所示(http://kickjava.com/src/javax/swing/plaf/basic/BasicTabbedPaneUI.java.htm#576 ) –

回答

0

通過重寫paintTab()方法,您要阻止標題從基類被塗。

你有兩個選擇之一:

  1. 覆蓋paintTabBackground()和paintTabBorder(),而不是重寫paintTab()方法,讓基類油漆標題
  2. 漆標題自己在你的paintTab()方法
+0

謝謝,我想我現在明白了! – Jama

+0

這不起作用... – Jama

相關問題