Java SWT Composite 1 px填充

时间:2013-10-21 14:56:04

标签: java swt

我在我的父Composite上使用了一个GridLayout,并想要删除渲染对象时创建的1px填充。要使此部件工作,要更改的参数是什么?我的复合材料就像这样呈现

final Composite note = new Composite(parent,SWT.BORDER);
GridLayout mainLayout = new GridLayout(1,true);
mainLayout.marginWidth = 0;
mainLayout.marginHeight = 0;
mainLayout.verticalSpacing = 0;
mainLayout.horizontalSpacing = 0;
note.setLayout(mainLayout);

图像:

enter image description here

1 个答案:

答案 0 :(得分:7)

SWT.BORDER导致您的问题。在Windows 7上,它将绘制2px,一个灰色和一个白色的边框。使用SWT.NONE完全摆脱边界。

如果您真的需要1px灰色边框,可以为Listener的父级SWT.Paint添加Composite,并使其与GC绘制边框:

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

    final Composite outer = new Composite(shell, SWT.NONE);
    outer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    GridLayout layout = new GridLayout(1, false);
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    outer.setLayout(layout);

    Composite inner = new Composite(outer, SWT.NONE);
    inner.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
    inner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    shell.addListener(SWT.Paint, new Listener()
    {
        public void handleEvent(Event e)
        {
            e.gc.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BORDER));
            Rectangle rect = outer.getBounds();
            Rectangle rect1 = new Rectangle(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2);
            e.gc.setLineStyle(SWT.LINE_SOLID);
            e.gc.fillRectangle(rect1);
        }
    });

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

看起来像这样:

enter image description here

这里有绿色背景:

enter image description here