在 Java 中将字符串转换为颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2854043/
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-08-13 13:40:09  来源:igfitidea点击:

Converting a String to Color in Java

javacolors

提问by devoured elysium

In .NET you can achieve something like this:

在 .NET 中,您可以实现以下目标:

Color yellowColor = Color.FromName("yellow");

Is there a way of doing this in Java without having to resort to reflection?

有没有办法在 Java 中做到这一点而不必求助于反射?

PS: I am not asking for alternative ways of storing/loading colors. I just want to know wherever it is possible to do this or not.

PS:我不是在要求存储/加载颜色的替代方法。我只是想知道哪里有可能做到这一点。

采纳答案by ZZ Coder

Use reflection to access the static member of the Colorclass.

使用反射访问类的静态成员Color

Color color;
try {
    Field field = Class.forName("java.awt.Color").getField("yellow");
    color = (Color)field.get(null);
} catch (Exception e) {
    color = null; // Not defined
}

回答by randy costanza

I tried something like this and it worked (at least for JavaFX)

我尝试过这样的事情并且它有效(至少对于 JavaFX)

String color = "red";
Color c = Color.web(color);
gc.setFill(color);
gc.fillOval(10, 10, 50, 40);

回答by blongho

Why not make a custom class for this? I did this and it is working for me. NB: You will have to include this class in your package.

为什么不为此创建一个自定义类?我这样做了,它对我有用。注意:您必须将此类包含在您的包中。

import java.awt.Color;

/**
 * A class to get the Color value from a string color name
 */
public class MyColor {
    private Color color;

 public MyColor(){
    color = Color.WHITE;
    }
/**
 * Get the color from a string name
 * 
 * @param col name of the color
 * @return White if no color is given, otherwise the Color object
 */
static Color getColor(String col) {
    switch (col.toLowerCase()) {
    case "black":
        color = Color.BLACK;
        break;
    case "blue":
        color = Color.BLUE;
        break;
    case "cyan":
        color = Color.CYAN;
        break;
    case "darkgray":
        color = Color.DARK_GRAY;
        break;
    case "gray":
        color = Color.GRAY;
        break;
    case "green":
        color = Color.GREEN;
        break;

    case "yellow":
        color = Color.YELLOW;
        break;
    case "lightgray":
        color = Color.LIGHT_GRAY;
        break;
    case "magneta":
        color = Color.MAGENTA;
        break;
    case "orange":
        color = Color.ORANGE;
        break;
    case "pink":
        color = Color.PINK;
        break;
    case "red":
        color = Color.RED;
        break;
    case "white":
        color = Color.WHITE;
        break;
        }
    return color;
    }
}

In some Containeri just call it like this

在某些情况下,Container我只是这样称呼它

public JPanel createStatusBar(){
    JPanel statusBar = new JPanel(layoutManager);
    statusBar.setBackgroundColr(MyColor.color("green"));
    // and other properties
    return statusBar;

Hope this helps.

希望这可以帮助。

回答by siordache

I open sourced a little library named AWT Color Factorythat provides methods for creating java.awt.Color instances from string representations.

我开源了一个名为AWT Color Factory的小库,它提供了从字符串表示创建 java.awt.Color 实例的方法。

These methods are the counterpart of static methods available in javafx.scene.paint.Color, such as Color.web(...) or Color.valueOf(...)

这些方法是 javafx.scene.paint.Color 中可用的静态方法的对应方法,例如 Color.web(...) 或 Color.valueOf(...)

The library is extremely lightweight and does not depend on JavaFX.

该库非常轻量级,不依赖于 JavaFX。