java 如何在RGB格式下在Java中设置标签的字体颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30507494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:14:41  来源:igfitidea点击:

How to set font color of a label in Java under RGB format?

javaswing

提问by TheMidnightNarwhal

How do I do something similar.

我如何做类似的事情。

lblPlay."set font color under RGB format"

lblPlay."设置RGB格式下的字体颜色"

I use Swing framework.

我使用 Swing 框架。

Thanks.

谢谢。

回答by Phylax Wetos

If you try to change the color of a JLabel inside of swing you can just go with

如果您尝试更改 Swing 内 JLabel 的颜色,您可以使用

import java.awt.Color;

and then use it like

然后像这样使用它

yourLabel.setForeground(Color.black) //For example black.

or construct the color with

或构造颜色

yourLabel.setForeground(new Color(0, 0, 0)) //Provide the r g b values

But if you want a more detailed answer you should provide a minimal example with compilable code or atleast describe where exactly the error happens.

但是如果你想要一个更详细的答案,你应该提供一个带有可编译代码的最小示例,或者至少描述错误发生的确切位置。

回答by Solano

Try to use:

尝试使用:

JLabel title = new JLabel("Give me color", JLabel.CENTER);
Color myCustomColor = new Color(100,50,2);
title.setForeground(myCustomColor);

回答by Steven Van Impe

If you're using JavaFX (which you should ;)), use either:

如果您使用的是 JavaFX(您应该使用 ;)),请使用:

label.setTextFill(Color.rgb(red, green, blue));

where red, greenand blueare integers. Or use:

其中redgreenblue是整数。或使用:

label.setTextFill(Color.web("rrggbb"));

where "rrggbb"is the typical hexademical RGB representation.

其中"rrggbb"是典型的十六进制 RGB 表示。