使用颜色代码设置摆动组件的前景

时间:2014-06-11 00:31:35

标签: java swing

有没有办法直接通过颜色代码而不是java提供的预定义颜色来设置标签的前景。

例如:

instead of:
SomeLabel.setForeground(Color.Red); 
We do:
SomeLabel.setForeground("240,240,240"); 

我问这个问题是因为当用户跳过它们时,我将所需字段的标签颜色设置为红色,所以当我想重置它们的颜色时,我找不到我在预定义内定义的颜色java提供的颜色。

2 个答案:

答案 0 :(得分:5)

您可以使用new Color(240, 240, 240)

请查看javax.swing.Color了解详情

您还可以在大多数外观上使用UIManager.getColor("Label.foreground");将标签重置为其外观和默认设置,例如

答案 1 :(得分:3)

SomeLabel.setForeground(new Color(240,240,240));

您可以使用java的Color类

您也可以添加Alpha等。请参阅下面的颜色构造函数

 Color(ColorSpace cspace, float[] components, float alpha)
Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.

Color(float r, float g, float b)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0).

Color(float r, float g, float b, float a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0).

Color(int rgb)
Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Color(int rgba, boolean hasalpha)
Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Color(int r, int g, int b)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255).

Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).