Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,正在努力返回颜色_Java_Colors_Enums - Fatal编程技术网

在Java中,正在努力返回颜色

在Java中,正在努力返回颜色,java,colors,enums,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 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;
我使用注释掉的messageblock进行了测试,得到了FColorText中所需的颜色(“测试用例中的绿色”)

My 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(int iRed, int iGreen, int iBlue, String text) {
    this.iRed   = iRed;
    this.iGreen = iGreen;
    this.iBlue  = iBlue;
    this.text  = text;
}
当我运行它时,它不是绿色的,而是白色的。知道为什么吗


非常感谢,

颜色
枚举中,
文本
属性始终为
null
:构造函数

Colors(String text) {
    this.text = text;
}
永远不会调用,因为所有颜色都是使用另一个采用RGB值的构造函数初始化的

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

if (text.equalsIgnoreCase(b.text))
因此,该方法总是返回
null
。然后我猜应用程序代码的其余部分认为
null
颜色是白色的

你有两个选择:

  • 修改
    颜色
    构造函数以包含
    文本
    参数,如下所示:

    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(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
。相反,您应该抛出一个特定的异常,比如
ColorNotFoundException
,或者抛出一条特定的消息。

Colors
枚举中,
text
属性总是
null
:构造函数

Colors(String text) {
    this.text = text;
}
永远不会调用,因为所有颜色都是使用另一个采用RGB值的构造函数初始化的

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

if (text.equalsIgnoreCase(b.text))
因此,该方法总是返回
null
。然后我猜应用程序代码的其余部分认为
null
颜色是白色的

你有两个选择:

  • 修改
    颜色
    构造函数以包含
    文本
    参数,如下所示:

    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(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
。相反,您应该抛出一个特定的异常,比如
ColorNotFoundException
,或者抛出一条特定的消息。

Colors
枚举中,
text
属性总是
null
:构造函数

Colors(String text) {
    this.text = text;
}
永远不会调用,因为所有颜色都是使用另一个采用RGB值的构造函数初始化的

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

if (text.equalsIgnoreCase(b.text))
因此,该方法总是返回
null
。然后我猜应用程序代码的其余部分认为
null
颜色是白色的

你有两个选择:

  • 修改
    颜色
    构造函数以包含
    文本
    参数,如下所示:

    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(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
。相反,您应该抛出一个特定的异常,比如
ColorNotFoundException
,或者抛出一条特定的消息。

Colors
枚举中,
text
属性总是
null
:构造函数

Colors(String text) {
    this.text = text;
}
永远不会调用,因为所有颜色都是使用另一个采用RGB值的构造函数初始化的

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

if (text.equalsIgnoreCase(b.text))
因此,该方法总是返回
null
。然后我猜应用程序代码的其余部分认为
null
颜色是白色的

你有两个选择:

  • 修改
    颜色
    构造函数以包含
    文本
    参数,如下所示:

    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(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
。相反,您应该抛出一个特定的异常,比如
ColorNotFoundException
,或者抛出一条特定的消息。

以下是最终对我有效的方法。出于某种原因,我在比较文本时遇到了问题(实际上,在我的系统中,“绿色”!=“绿色”)


这就是最终对我有效的方法。出于某种原因,我在比较文本时遇到了问题(实际上,在我的系统中,“绿色”!=“绿色”)


这就是最终对我有效的方法。出于某种原因,我在比较文本时遇到了问题(实际上,在我的系统中,“绿色”!=“绿色”)


这就是最终对我有效的方法。出于某种原因,我在比较文本时遇到了问题(实际上,在我的系统中,“绿色”!=“绿色”)


请创建并发布一个。另外,为什么不给每个颜色项一个颜色字段,一个根据3个int常量计算的字段?这里似乎有一个基本错误?一个枚举有两个构造函数,它们都不是另一个的方便构造函数。这在我脑子里敲响了警钟。将所有字段设置为final,然后查看是否暴露任何新错误。就这样@沃伦有你的问题和解决方案在手!请创建并发布一个。另外,为什么不给每个颜色项一个颜色字段,一个计算器