GridLayout的setLineBackground

时间:2013-11-28 13:34:57

标签: java user-interface plugins background swt

我想为Eclipse插件SWT GUI创建斑马背景。 一行灰色,一行白色,一行灰色等。 所以我想为每一行like this设置背景。

但是我不想使用StyledText,当我在线上的每个组件上设置背景时,会出现间隙。组件背后的标签不是一个选项,因为SWT不知道深度。

那么如何更改GridLayout中整行的背景颜色?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

此代码适用于我:

private static final Color[] colors = new Color[2];

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

    Composite content = new Composite(shell, SWT.NONE);
    GridLayout layout = new GridLayout(1, false);
    layout.verticalSpacing = 0;
    content.setLayout(layout);
    content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    colors[0] = display.getSystemColor(SWT.COLOR_DARK_GRAY);
    colors[1] = display.getSystemColor(SWT.COLOR_GRAY);

    for (int i = 0; i < 5; i++)
        createPart(content, i);

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

private static void createPart(Composite parent, int i)
{
    Composite comp = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(2, false);
    layout.marginHeight = 0;
    comp.setLayout(layout);
    comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    comp.setBackground(colors[i % 2]);

    Label left = new Label(comp, SWT.NONE);
    left.setText("Left");
    left.setBackground(colors[i % 2]);
    Label right = new Label(comp, SWT.NONE);
    right.setText("Right");
    right.setBackground(colors[i % 2]);
}

看起来像这样:

enter image description here