gpt4 book ai didi

java - GridLayout 的 setLineBackground

转载 作者:行者123 更新时间:2023-12-01 13:47:17 25 4
gpt4 key购买 nike

我想为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com