java - 在 Java 中,努力返回颜色

标签 java colors enums

因此,在我的主课中,我正在执行以下操作:

String myString = msgBlock.getMsg();
Color fColor = Color.WHITE;
msgBlock.setSuppressed(TernarySwitch.ON); /* suppress original message to display new one */
String[] myStringParts = myString.split("\\s+",13); /*divide into 13 parts */
String finalPart = myStringParts[12].toString(); /* print last part */
String fColorMsg     = myStringParts[7].toString();
String[] fColorParts = fColorMsg.split("_",2);
String fColorTxt     = fColorParts[1].toString();
fColor               = Colors.fromString(fColorTxt);
/*MessageBlock mb = new MessageBlock(fColorTxt, Constants.ETOS_ONE_MSG);*/
MessageBlock mb = new MessageBlock(finalPart, Constants.ETOS_ONE_MSG);
mb.setForeground(fColor);
fw.addFilteredMessage(mb);
return msgBlock;

我使用注释掉的消息 block 进行了测试,并且在 fColorTxt 中获得了我需要的颜色(在我的测试用例中为“绿色”)。

我的 Colors.Java 看起来像这样:

package com.ibm.tpf.internal;

import java.awt.Color;

public enum Colors{
    BLACK   (  0,   0,   0),
    BLUE    (  0,   0, 255), LIGHT_BLUE    (  0, 128, 255), DARK_BLUE    (  0,   0, 128),
    BROWN   (160,  96,   0), LIGHT_BROWN   (208, 144,   0), DARK_BROWN   ( 96,  32,   0),
    CYAN    (  0, 255, 255), LIGHT_CYAN    (176, 255, 255), DARK_CYAN    (  0, 139, 139),
    GRAY    (128, 128, 128), LIGHT_GRAY    (211, 211, 211), DARK_GRAY    ( 64,  64,  64),
    GREY    (128, 128, 128), LIGHT_GREY    (211, 211, 211), DARK_GREY    ( 64,  64,  64),
    GREEN   (  0, 255,   0), LIGHT_GREEN   (128, 255, 128), DARK_GREEN   (  0, 128,   0),
    MAGENTA (255,   0, 255), LIGHT_MAGENTA (255, 144, 255), DARK_MAGENTA (144,   0, 144),
    MINT    ( 96, 221,  96), LIGHT_MINT    (208, 238, 208), DARK_MINT    ( 16, 187,  16),
    ORANGE  (255, 128,   0), LIGHT_ORANGE  (255, 176,  48), DARK_ORANGE  (192,  64,   0),
    PINK    (255, 192, 203), LIGHT_PINK    (255, 128, 255), DARK_PINK    (231,  84, 128),
    YELLOW  (255, 255,   0), LIGHT_YELLOW  (255, 255, 128), DARK_YELLOW  (160, 160,   0),
    WHITE   (255, 255, 255);

    private int iRed;
    private int iGreen;
    private int iBlue;
    private String text;

    Colors(int iRed, int iGreen, int iBlue) {
        this.iRed   = iRed;
        this.iGreen = iGreen;
        this.iBlue  = iBlue;
    }

    Colors(String text) {
        this.text = text;
    }

    public String getText() {
        return this.text;
    }

    public static Color fromString(String text) {
        if (text != null) {
            for (Colors b : Colors.values()) {
                if (text.equalsIgnoreCase(b.text)) {
                    return new Color (b.iRed, b.iBlue, b.iGreen);
                }
            }
        }
        return null;
    }
} 

当我运行它时,它不是绿色的,而是白色的。知道为什么会这样吗?

非常感谢

最佳答案

Colors 枚举中,text 属性始终为 null:构造函数

Colors(String text) {
    this.text = text;
}

永远不会被调用,因为所有颜色都是使用另一个采用 RGB 值的构造函数来初始化的。

由于 text 始终为 null,因此 fromString 中的 if 语句始终返回 false:

if (text.equalsIgnoreCase(b.text))

因此该方法始终返回null。然后我猜应用程序代码的其余部分认为 null 颜色是白色。

你有两个选择:

  • 修改您的 Colors 构造函数以包含 text 参数,如下所示:

    Colors(int iRed, int iGreen, int iBlue, String text) {
        this.iRed   = iRed;
        this.iGreen = iGreen;
        this.iBlue  = iBlue;
        this.text  = text;
    }
    

    然后,每个枚举声明将变为:

    BLACK   (  0,   0,   0, "black")
    
  • 删除 text 参数并构建代码,使其等于枚举的 name()。然后,fromString 方法变为:

    public static Color fromString(String text) {
        if (text != null) {
            for (Colors b : Colors.values()) {
                if (b.name().equalsIgnoreCase(b.text)) {
                    return new Color (b.iRed, b.iBlue, b.iGreen);
                }
            }
        }
        return null;
    }
    

请注意,当 fromString 无法识别颜色时,您不应返回 null。相反,您应该抛出一个特定的异常,例如 ColorNotFoundExceptionIllegalArgumentException带有特定消息。

关于java - 在 Java 中,努力返回颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32212501/

相关文章:

java - 井字棋盘,WIN方法错误

python - Matplotlib 散点图 - ValueError : RGBA sequence should have length 3 or 4

matplotlib - matplotlib 标记中的多种颜色填充

scala:向枚举添加方法

java - Saxon 9.6.0.7版本也需要saxon-dom?

java - XSLT 转换有时会失败

Javafx - 从节点的颜色填充获取 RGB 值

swift - 代替可失败的初始化器,你可以让它返回一个默认情况吗?

ios - 使用自定义枚举作为值在 Swift 中创建字典 'on-the-fly'

java - 达到警告时 Intellij 条件始终为 false