在Swing中,是否有一个属性可以设置禁用JList的前景色? [英] In Swing, is there a property to set a JList disabled foreground color?

查看:64
本文介绍了在Swing中,是否有一个属性可以设置禁用JList的前景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swing中,是否有一个属性可以设置禁用JList的前景色?

In Swing, is there a property to set a JList disabled foreground color?

我正在使用Netbeans GUI构建器,我想向资源属性文件添加一个属性,该属性在禁用JList时将JList中文本的颜色设置为其他默认值.使用Nimbus LAF可以使用不同的颜色,因为禁用时文本为灰色,启用时为黑色.我只是不希望它在禁用时为灰色.

I'm using the Netbeans GUI builder and I want to add a property to the resource properties file that sets the color of the text in a JList to a different default when the JList is disabled. Using the Nimbus LAF, there is a different color for this because the text is gray when disabled but black when enabled. I just don't want it to be grey when disabled.

标准前景色是.foreground.

The standard foreground color is just .foreground.

谢谢.

推荐答案

DefaultListCellRenderer扩展了JLabel.在其getListRendererComponent方法中,它根据传入的JList的状态来设置其启用状态.

The DefaultListCellRenderer extends JLabel. In its getListRendererComponent method, it sets its enabled state based on that of the JList that is passed in.

BasicLabelUI中用于绘制禁用的JLabel文本的代码可以做一些工作来用阴影效果绘制文本.在许多子类中,您将找到寻找"Label.disabledForeground" UI属性的代码. Nimbus默认值似乎在寻找"Label.disabledText"

The code for painting disabled JLabel text in BasicLabelUI does some work to paint the text with a shadow effect. In many subclasses, you will find code that looks for the "Label.disabledForeground" UI property. The Nimbus defaults seem to look for "Label.disabledText".

您有两种选择:

  1. 您可以在UIManager中设置"Label.disabledText"属性,这将使所有JLabel实例和子类被禁止进行这种着色.
  2. 您可以为JList创建一个自定义渲染器,以测试JList的启用状态,然后执行您想要的任何自定义代码-或完全省略启用状态测试(如果您希望它看起来相同)不论其启用状态.
  1. You can set the "Label.disabledText" property in the UIManager, which will make all JLabel instances and subclasses that are disabled to take on this coloring.
  2. You can create a custom renderer for your JList that tests the enabled state of the JList and then does whatever custom code that you would like - or omit the enabled state test entirely if you want it to look the same regardless of its enabled state.

我建议您使用自定义渲染器方法,因为很难说出JLabel属性的更改将显示在何处,因为该类在许多不同组件中都用作构建块.

I would suggest that you take the custom renderer approach, as it is difficult to say where the change of a JLabel property will show up, as that class is used as a builing block in many different components.

这篇关于在Swing中,是否有一个属性可以设置禁用JList的前景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆