有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java颜色。getColor(“红色”)返回空值

我希望用户导入一个字符串,比如“红色”,然后能够将其传递给下面的代码

String red = "Red";
Color color = Color.getColor(red);
System.out.println(color);

output: null

我如何让它输出红色。如果可能的话


共 (2) 个答案

  1. # 1 楼答案

    看看JavaDoc,上面写着什么:

    Finds a color in the system properties. The argument is treated as the name of a system property to be obtained. The string value of this property is then interpreted as an integer which is then converted to a Color object.

    If the specified property is not found or could not be parsed as an integer then null is returned.

    Parameters:
    nm - the name of the color property 
    Returns:
    the Color converted from the system property.
    

    “红色”未定义为系统属性