是否可以在SWT中更改两个嵌套复合材料顶侧之间的间距?
例如,当使用下面的代码时,我得到的结果如下所示:
我希望红色和蓝色复合材料的顶部之间没有任何空间,如下所示:
我尝试使两个组件的高度相同(使用GridData
),但这不起作用。
public class CompositeSnippet
{
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Composite c1 = new Composite(shell, SWT.NONE);
GridData c1GridData = new GridData(100, 50);
c1.setLayoutData(c1GridData);
c1.setLayout(new GridLayout());
c1.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Composite c2 = new Composite(c1, SWT.NONE);
GridData c2GridData = new GridData(50, 50);
c2.setLayoutData(c2GridData);
c2.setBackground(display.getSystemColor(SWT.COLOR_RED));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
基于layouts in SWT上的这篇文章,似乎这个间距可能是由于蓝色复合材料的修剪。我试图创建自己的自定义小部件,扩展Composite并覆盖computeTrim(...)
方法,但这也不起作用......
P.S。似乎已经问过这个问题了,但我找不到与它相关的 stackoverflow 上的任何内容。如果已经解决了这个问题,请指出正确的讨论,我为重复的帖子道歉。
答案 0 :(得分:2)
你走了:
public static void main(String[] args)
{
final Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final Composite outer = new Composite(shell, SWT.NONE);
outer.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = 0;
outer.setLayout(layout);
outer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Composite inner = new Composite(outer, SWT.NONE);
inner.setBackground(display.getSystemColor(SWT.COLOR_RED));
inner.setLayout(new GridLayout(1, false));
inner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
只需使用GridLayout#marginHeight
并将其设置为0
。
看起来像这样:
答案 1 :(得分:0)
您需要将复合c1的GridLayout.marginHeight
设置为0。
public class CompositeSnippet
{
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Composite c1 = new Composite(shell, SWT.NONE);
GridData c1GridData = new GridData(100, 50);
c1.setLayoutData(c1GridData);
//Set marginWidth and marginHeight of parent composite to 0
GridLayout c1Layout = new GridLayout();
c1Layout.marginHeight = 0;
c1.setLayout(c1Layout);
c1.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Composite c2 = new Composite(c1, SWT.NONE);
GridData c2GridData = new GridData(50, 50);
c2.setLayoutData(c2GridData);
c2.setBackground(display.getSystemColor(SWT.COLOR_RED));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}