java - GridLayout 的 setLineBackground

标签 java user-interface plugins background swt

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

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

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

感谢您的帮助:)

最佳答案

这段代码对我有用:

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

关于java - GridLayout 的 setLineBackground,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20267607/

相关文章:

java - java中如何将字符串与通配符匹配

java - 从服务器下载数据

java, generics - 如何参数化一个类型为 T 的类,这两个类型都是 : extends and implements sth

javascript - "smart home"物联网 WebSocket --> 服务器 WebSocket --> 客户端

python - 为什么 PyGtk 中的 set_model 方法将第一列的值复制到第三列?

eclipse - 在 GWT 2.1.0.M2 中,无法识别选项 "-style pretty"

java - eclipse中的sql更新查询

java - 从另一个线程实时更新 JLabel 文本

java - Google Eclipse 插件 : Browser Plugin vs. 开发模式

javascript - Visual Studio Web 部署项目自动压缩 js 和 css