本文介绍了在空白文本框中选择提示/消息的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本框为空时创建带有提示的文本框.我使用 setMessage方法,它可以正常工作.如何更改提示的默认颜色?

I want to create Text box with prompt when it is empty. I use setMessage method and it's working fine. How can I change default color of prompt?

推荐答案

如果要更改提示的颜色,只需在焦点事件中添加 Listener .

If you want to change the color of the prompt, just add a Listener to the focus events.

public class StackOverflow
{
    public static void main(String[] args)
    {
        final Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("StackOverflow");
        shell.setLayout(new GridLayout(1, true));

        final Text text = new Text(shell, SWT.BORDER | SWT.SEARCH);
        text.setForeground(display.getSystemColor(SWT.COLOR_RED));
        text.setText("Enter something");
        text.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, true));

        text.addListener(SWT.FocusOut, new Listener()
        {
            @Override
            public void handleEvent(Event arg0)
            {
                if("".equals(text.getText()))
                {
                    text.setForeground(display.getSystemColor(SWT.COLOR_RED));
                    text.setText("Enter something");
                }
            }
        });

        text.addListener(SWT.FocusIn, new Listener()
        {
            @Override
            public void handleEvent(Event arg0)
            {

                if("Enter something".equals(text.getText()))
                {
                    text.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
                    text.setText("");
                }
            }
        });

        Label label = new Label(shell, SWT.NONE);
        label.setFocus();
        label.forceFocus();

        shell.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

不专心/空:

关注/不为空

如您所见,提示"现在为红色.

As you can see, the "prompt" is now red.

这篇关于在空白文本框中选择提示/消息的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:06