所以在我的主要课程中,我正在做以下事情:
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;
我使用注释掉的消息块进行了测试,我得到了fColorTxt中需要的颜色(在我的测试用例中为“GREEN”)。
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;
}
}
当我跑它时,它不是绿色的,它是白色的。知道为什么会这样吗?
非常感谢,
答案 0 :(得分:3)
在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;
}
请注意,null
无法识别颜色时,您不应返回fromString
。相反,您应该抛出一个特定的异常,例如ColorNotFoundException
或IllegalArgumentException
特定的消息。
答案 1 :(得分:0)
所以这里最终对我有用。出于某种原因,我在比较文本时遇到了问题(实际上," GREEN"!=" GREEN"在我的系统中)。
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),
RED (255, 0, 0), LIGHT_RED (255, 128, 128), DARK_RED (128, 0, 0),
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;
Colors(int iRed, int iGreen, int iBlue) {
this.iRed = iRed;
this.iGreen = iGreen;
this.iBlue = iBlue;
}
public static Color fromString(String text) {
if (text != null) {
Colors ret = Colors.valueOf(text.toUpperCase());
return new Color (ret.iRed, ret.iGreen, ret.iBlue);
}
return null;
}
}