The Color class contains a number of predefined colors such as red and green. This example demonstrates how to retrieve one of these predefined colors using a string. For example, the predefined color java.awt.Color.red could be retrieved with the string "red".
// Returns a Color based on 'colorName' which must be one of the predefined colors in
// java.awt.Color. Returns null if colorName is not valid.
public Color getColor(String colorName) {
try {
// Find the field and value of colorName
Field field = Class.forName("java.awt.Color").getField(colorName);
return (Color)field.get(null);
} catch (Exception e) {
return null;
}
}
|
e584. Retrieving a Predefined Color by Name
最新推荐文章于 2021-01-18 23:58:22 发布
本文介绍了一种方法,可以通过字符串名称从Java AWT Color类中检索预定义的颜色。例如,使用字符串red可以获取到预定义的颜色红色。此方法为开发者提供了灵活的色彩选择方式。
7186

被折叠的 条评论
为什么被折叠?



