Java的颜色从字符串"黄色" [英] Java Color from String "Yellow"
问题描述
有什么办法,我们可以从字符串得到的颜色(如白)?
Is there any way we can get color from String (like "White")?
Color color;
Field field = Class.forName("java.awt.Color").getField("Yellow");
color = (Color)field.get(null);
我试过在Java中并成为一个字符串转换为彩色抛出错误。
什么场是属于?我需要什么包导入呢?
I tried Converting a String to Color in Java and it throws error . What "Field" belongs to? What package do I need to import for it?
推荐答案
这是因为定义黄色的领域被命名为黄
或黄
It is because the field that defines yellow is named YELLOW
or yellow
您有一个大写Y,不能被映射到一个颜色。相反,尝试:
You have an uppercase Y, which cannot be mapped to a Color. Instead, try:
Field field = Class.forName("java.awt.Color").getField("yellow");
看这个类包含的颜色的所有字段
<一href=\"http://download.oracle.com/javase/6/docs/api/java/awt/Color.html\">http://download.oracle.com/javase/6/docs/api/java/awt/Color.html
在code只是使用反射来访问这些领域之一。
The code is just using reflection to access one of these fields.
颜色列表但是是相当有限的,所以我不知道有多少使用这很可能是你。
The list of colours however is quite limited, so I don't know how much use this is likely to be for you.
这篇关于Java的颜色从字符串&QUOT;黄色&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!