更新时间:2023-01-27 14:47:52
如果您的名字与Java的常量相对应,您可以使用反射来映射它们:
If your names correspond to those of Java's constants, you can use reflection to map them:
public static Color getColorByName(String name) {
try {
return (Color)Color.class.getField(name.toUpperCase()).get(null);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
e.printStackTrace();
return null;
}
}