Java 使用颜色代码设置回转组件的前地面

Java 使用颜色代码设置回转组件的前地面,java,swing,Java,Swing,是否有任何方法可以直接通过颜色代码而不是java提供的预定义颜色来设置标签的前景 例如: instead of: SomeLabel.setForeground(Color.Red); We do: SomeLabel.setForeground("240,240,240"); 我问这个问题是因为当用户跳过所需字段时,我会将它们的标签颜色设置为红色,所以当我想重置它们的颜色时,我无法在java提供的预定义颜色中找到我定义的颜色 您可以使用新颜色(240240240) 请查看以了解更多详细信

是否有任何方法可以直接通过颜色代码而不是java提供的预定义颜色来设置标签的前景

例如:

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

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

您可以使用
新颜色(240240240)

请查看以了解更多详细信息


您还可以使用
UIManager.getColor(“Label.foreground”)将标签重置回其外观默认值在大多数外观上,例如

您可以使用
新颜色(240240240)

SomeLabel.setForeground(new Color(240,240,240));
请查看以了解更多详细信息

您还可以使用
UIManager.getColor(“Label.foreground”)将标签重置回其外观默认值

SomeLabel.setForeground(new Color(240,240,240));
您可以使用java的颜色类

您还可以添加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).
您可以使用java的颜色类

您还可以添加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).

有一件事让程序员感到困惑,我只是注意到它是java.awt.Color,它工作得非常好,但是出于好奇问这个问题,还有javax.swing.Color吗?我在搜索时找不到它。@FlowFlowFlowFlowFlowFlow不,Swing大量借用了AWT,实际上使生活变得更简单,因为你不需要一直怀疑自己是否有合适的类……它也可能有点令人沮丧(
Image
ImageIcon
Icon
BufferedImage
…:P)有一件事让程序员感到困惑,我只是注意到它是java.awt.Color,它工作得非常好,但是出于好奇问这个问题,还有javax.swing.Color吗?我在搜索时找不到它。@FlowFlowFlowFlowFlowFlow不,Swing大量借用了AWT,实际上使生活变得更简单,因为你不需要一直怀疑自己是否有合适的类……它也可能有点令人沮丧(
Image
ImageIcon
Icon
BufferedImage
…:P)