【问题标题】:Can you explicitly set the background color of a SWT Text widget to the default color?您可以将 SWT Text 小部件的背景颜色显式设置为默认颜色吗?
【发布时间】:2012-02-13 21:06:23
【问题描述】:

我的 UI 中有一个 SWT Text 字段。如果文本字段包含不寻常的值(由我的特定用例确定),我会将背景颜色设置为红色,以引起用户的注意。如果文本字段的值(文本)更改为正常值,我想将背景颜色设置回默认值,但我认为这是不可能的。

有可能吗?我可以将 SWT 文本小部件的背景颜色显式更改回默认值吗?

【问题讨论】:

    标签: java eclipse widget swt


    【解决方案1】:

    尝试将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. 所以这个答案肯定是正确的。
    【解决方案2】:

    我认为没有内置方法可以获取“原始颜色”。我的建议是保留一个具有旧值的字段,并在需要时返回它:

    class FlashingText extends Text{
        //Enter needed ctors
        private Color originalColor;
        public void markForUser(){
            originalColor = getBackground();
            setBackground(Color.RED);
        }
        public void resetColor(){
            setBackground(originalColor);
        }
    }
    

    请注意,即使您将来出于设计目的决定更改文本的颜色,这也将起作用。另请注意,在您的程序中为实体提供对象通常是好的设计。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 2014-10-20
      • 2013-10-26
      • 2022-10-18
      • 2016-05-24
      • 2014-11-03
      • 2011-10-09
      相关资源
      最近更新 更多