来自字符串“Yellow”的 Java 颜色
有什么方法可以从字符串中获取颜色(例如“白色”)?
Color color;
Field field = Class.forName("java.awt.Color").getField("Yellow");
color = (Color)field.get(null);
我尝试 在 Java 中将字符串转换为颜色 并抛出 error 。 “领域”属于什么?我需要导入什么包?
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);
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为定义黄色的字段被命名为
YELLOW
或yellow
您有一个大写的 Y,它无法映射到颜色。相反,请尝试:
查看此类以获取 Color 中包含的所有字段
http://download.oracle.com/javase/6 /docs/api/java/awt/Color.html
代码只是使用反射来访问这些字段之一。
然而,颜色列表非常有限,所以我不知道这对您来说有多大用处。
It is because the field that defines yellow is named
YELLOW
oryellow
You have an uppercase Y, which cannot be mapped to a Color. Instead, try:
Look at this class for all the fields contained within Color
http://download.oracle.com/javase/6/docs/api/java/awt/Color.html
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.
“黄”不是“黄”
"yellow" not "Yellow"