【发布时间】:2012-02-13 21:06:23
【问题描述】:
我的 UI 中有一个 SWT Text 字段。如果文本字段包含不寻常的值(由我的特定用例确定),我会将背景颜色设置为红色,以引起用户的注意。如果文本字段的值(文本)更改为正常值,我想将背景颜色设置回默认值,但我认为这是不可能的。
有可能吗?我可以将 SWT 文本小部件的背景颜色显式更改回默认值吗?
【问题讨论】:
我的 UI 中有一个 SWT Text 字段。如果文本字段包含不寻常的值(由我的特定用例确定),我会将背景颜色设置为红色,以引起用户的注意。如果文本字段的值(文本)更改为正常值,我想将背景颜色设置回默认值,但我认为这是不可能的。
有可能吗?我可以将 SWT 文本小部件的背景颜色显式更改回默认值吗?
【问题讨论】:
尝试将null 的Color 参数传递给setBackground:
text.setBackground(null);
【讨论】:
Sets the receiver's background color to the color specified by the argument, or to the default system color for the control if the argument is null. 所以这个答案肯定是正确的。
我认为没有内置方法可以获取“原始颜色”。我的建议是保留一个具有旧值的字段,并在需要时返回它:
class FlashingText extends Text{
//Enter needed ctors
private Color originalColor;
public void markForUser(){
originalColor = getBackground();
setBackground(Color.RED);
}
public void resetColor(){
setBackground(originalColor);
}
}
请注意,即使您将来出于设计目的决定更改文本的颜色,这也将起作用。另请注意,在您的程序中为实体提供对象通常是好的设计。
【讨论】: