2010-01-06 59 views
1

使用Synth LaF,我無法爲DISABLED狀態設置JLabel的FOREGROUND顏色。有沒有人成功做到這一點?這是我的LaF.xml文件中的標籤樣式定義。Synth LaF JLabel DISABLED顏色

<style id="whiteLabelStyle"> 
     <opaque value="false"/> 
     <font name="Bitstream Vera Sans" size="16" /> 
     <state> 
      <color type="FOREGROUND" value="WHITE"/> 
     </state> 
     <state value="DISABLED"> 
      <color type="FOREGROUND" value="BLACK"/> 
     </state> 
    </style> 
    <bind style="whiteLabelStyle" type="name" key="WhiteOrbitLabel"/> 

請不是在我的LaF.xml文件中定義的其他樣式在我的應用程序,包括我的標籤的白正常狀態下的顏色呈現正確(這只是從來沒有去黑當我做lbl.setEnabled(假)

而且,通過合成器的代碼去,我發現下面的評論中SynthStyle.getColor

 if ((context.getComponentState() & SynthConstants.DISABLED) != 0) { 
     //This component is disabled, so return the disabled color. 
     //In some cases this means ignoring the color specified by the 
     //developer on the component. In other cases it means using a 
     //specified disabledTextColor, such as on JTextComponents. 
     //For example, JLabel doesn't specify a disabled color that the 
     //developer can set, yet it should have a disabled color to the 
     //text when the label is disabled. This code allows for that. 
     if (c instanceof JTextComponent) { 
      JTextComponent txt = (JTextComponent)c; 
      Color disabledColor = txt.getDisabledTextColor(); 
      if (disabledColor == null || disabledColor instanceof UIResource) { 
       return getColorForState(context, type); 
      } 
     } else if (c instanceof JLabel 
       && (type == ColorType.FOREGROUND || type == ColorType.TEXT_FOREGROUND)){ 
      return getColorForState(context, type); 
     } 

但我無法弄清楚如何設置禁用顏色的JLabel

感謝您的幫助!

回答

0

我知道這個問題是舊的,但也許有人還是需要的答案:

要自定義文本顏色Synth中的大號&男,你需要設置顏色類型爲「TEXT_FOREGROUND」,像這樣:

<state value="DISABLED"> 
    <color type="TEXT_FOREGROUND" value="BLACK"/> 
</state> 
+0

不幸的是,TEXT_FOREGROUND不起作用Ë ither。事實上,如果你看看我從Synth代碼中粘貼的代碼片段,你可以看到設置FOREGROUND或TEXT_FOREGROUND具有相同的效果。 – mmoris 2010-05-12 13:38:53

+0

您粘貼的代碼片段與Sun的JDK 1.5中的代碼片段不同(我將它粘貼到下一個答案中)。我們公司已經交付了開源的基於Synth的L&F,解決了這個問題。你可以谷歌「EaSynth外觀和感覺」。 – 2010-05-13 02:05:53

0

我發現SynthStyle.getColor源代碼比你的不同(我的是來自Sun JDK 1.5):

/** 
* Returns the color for the specified state. This gives precedence to 
* foreground and background of the <code>JComponent</code>. If the 
* <code>Color</code> from the <code>JComponent</code> is not appropriate, 
* or not used, this will invoke <code>getColorForState</code>. Subclasses 
* should generally not have to override this, instead override 
* {@link #getColorForState}. 
* 
* @param context SynthContext identifying requester 
* @param type Type of color being requested. 
* @return Color 
*/ 
public Color getColor(SynthContext context, ColorType type) { 
    JComponent c = context.getComponent(); 
    Region id = context.getRegion(); 
    int cs = context.getComponentState(); 
    // For the enabled state, prefer the widget's colors 
    if (!id.isSubregion() && cs == SynthConstants.ENABLED) { 
     if (type == ColorType.BACKGROUND) { 
      return c.getBackground(); 
     } 
     else if (type == ColorType.FOREGROUND) { 
      return c.getForeground(); 
     } 
     else if (type == ColorType.TEXT_FOREGROUND) { 
      // If getForeground returns a non-UIResource it means the 
      // developer has explicitly set the foreground, use it over 
      // that of TEXT_FOREGROUND as that is typically the expected 
      // behavior. 
      Color color = c.getForeground(); 
      if (!(color instanceof UIResource)) { 
       return color; 
      } 
     } 
    } 
    // Then use what we've locally defined 
    Color color = getColorForState(context, type); 
    if (color == null) { 
     // No color, fallback to that of the widget. 
     if (type == ColorType.BACKGROUND || 
        type == ColorType.TEXT_BACKGROUND) { 
      return c.getBackground(); 
     } 
     else if (type == ColorType.FOREGROUND || 
       type == ColorType.TEXT_FOREGROUND) { 
      return c.getForeground(); 
     } 
    } 
    return color; 
}