SWT从Composite中删除保证金

时间:2015-05-28 18:39:25

标签: java layout swt

当我在Composite中嵌入ViewPart控件时,在文本框和按钮之间添加了一个垂直边距/填充(按钮是另一个复合材料的一部分),如附图所示。

如何删除它?

public class View extends ViewPart {
    public static final String ID = "xyyx.view";

    public void createPartControl(Composite parent) {

        GridLayout layout = new GridLayout(1, false);
        parent.setLayout(layout);


        Text infoText = new Text(parent, SWT.BORDER);

        GridData gridDataInfo = new GridData();
        gridDataInfo.horizontalSpan = 1;
        gridDataInfo.grabExcessHorizontalSpace = true;
        gridDataInfo.horizontalAlignment = GridData.FILL;
        infoText.setLayoutData(gridDataInfo);       

        new NewComposite(parent, SWT.NONE);         

    }

    public class NewComposite extends Composite{

        public NewComposite(Composite parent, int style) {
            super(parent, style);           

            GridLayout layout = new GridLayout(1, false);
            setLayout(layout);      

            Button btn = new Button(parent, SWT.PUSH);
            btn.setText("Button");                  
        }

    }

    @Override
    public void setFocus() {
        // TODO Auto-generated method stub

    }

}

enter image description here

2 个答案:

答案 0 :(得分:8)

GridLayout#marginHeightGridLayout#marginWidth可以设置为0以删除边距。

在您的情况下,设置GridLayout#marginTop的{​​{1}}就足够了。

JavaDoc of GridLayout#marginHeight

像这样:

NewComposite

看起来像这样:

enter image description here

如果您想进一步缩小合成间距,可以将public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, false)); Composite first = new Composite(shell, SWT.NONE); Composite second = new Composite(shell, SWT.NONE); GridLayout firstLayout = new GridLayout(1, false); first.setLayout(firstLayout); GridLayout secondLayout = new GridLayout(1, false); secondLayout.marginHeight = 0; secondLayout.marginWidth = 0; second.setLayout(secondLayout); Button firstButton = new Button(first, SWT.PUSH); firstButton.setText("Margin"); Button secondButton = new Button(second, SWT.PUSH); secondButton.setText("No Margin"); first.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); second.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); firstButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); secondButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } 设置为GridLayout#verticalSpacing

0

这将是这样的:

enter image description here

答案 1 :(得分:0)

GridLayout#horizo​​ntalSpacing GridLayout#verticalSpacing 可以设置为0以删除列和行之间的间距。

在您设置GridLayout#case的情况下,父组合的verticalSpacing应该可以解决问题。