2011-10-26 12 views
0

我会扩大BasicTabbedPaneUi,所以我可以设计我自己的tabPane。我有一个与HTML文本有关的问题,就是在选中标签后设置文本的颜色。我重写paintText方法与下面几乎相同的代码作为一部开拓创新的方法:BasicTabbedPaneUI画html文本

@Override 
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) { 
    g.setFont(font); 

    View v = getTextViewForTab(tabIndex); 
    if (v != null) { 
     // html 
     Color fg = tabPane.getForegroundAt(tabIndex); 
     if (isSelected && (fg instanceof UIResource)) { 
      Color selectedFG = UIManager.getColor(
        "TabbedPane.selectedForeground"); 
      if (selectedFG != null) { 
       fg = selectedFG; 
      } 
     } 
     v.paint(g, textRect); 
    } else { 
     // plain text 
     int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex); 

     if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) { 
      Color fg = tabPane.getForegroundAt(tabIndex); 
      if (isSelected && (fg instanceof UIResource)) { 
       Color selectedFG = UIManager.getColor(
         "TabbedPane.selectedForeground"); 
       if (selectedFG != null) { 
        fg = selectedFG; 
       } 
      } 
      g.setColor(fg); 
      SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, 
        title, mnemIndex, 
        textRect.x, textRect.y + metrics.getAscent()); 

     } else { // tab disabled 
      g.setColor(tabPane.getBackgroundAt(tabIndex).brighter()); 
      SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, 
        title, mnemIndex, 
        textRect.x, textRect.y + metrics.getAscent()); 
      g.setColor(tabPane.getBackgroundAt(tabIndex).darker()); 
      SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, 
        title, mnemIndex, 
        textRect.x - 1, textRect.y + metrics.getAscent() - 1); 

     } 
    } 
} 

在这种情况下,我们有HTML文本的标签,它v不空。如果我为绘画方法中使用的图形对象设置颜色,它不会更改文本颜色。 我使用的HTML,因为我想我的标签的文本在两行。 感谢您帮助改变颜色。

+0

设置HTML [color](http://www.w3.org/TR/REC-html32)属性时会发生什么? – trashgod

+0

它工作时,我直接在html标记中设置它,但我怎么能改变它当选项卡被选中? – xtrem06

回答

1

我会不愿意开发自定义TabbedPaneUI,除非它是一个完整的外观&感受implmentation的一部分。

取而代之,考虑一个自定义选项卡组件,如TabComponentsDemo中所示,并在How to Use Tabbed Panes中讨论。这将使您可以绝对控制组件的外观,而不会牺牲与用户选择的外观的兼容性。

+0

+1外观和感觉清晰 – mKorbel