我可以组合SWT GridLayout和FillLayout

时间:2013-05-30 21:56:58

标签: java swt eclipse-rcp

我有一个RCP / SWT应用程序,我正在尝试用现有的复合材料构建一个视图。一个是FillLayout复合,另一个是GridLayout。

我想最终得到一个视图,其中GridLayout复合排列在FillLayout合成的左边(想想垂直横幅),大约是整个视图宽度的10%,现有的FillLayout复合材料包含其他90%。

我不确定在SWT中是否可以组合布局,但我正在考虑类似具有两列的GridLayout。第一列包含GridLayout小部件,第二列包含FillLayout组合。这可以在SWT中完成吗?如果是这样,这看起来像代码?

谢谢 -

2 个答案:

答案 0 :(得分:7)

开始为您的外部合成设置FormLayout。在其中放置另外两个复合材料,设置FormData信息以便根据需要定位它们。然后设置这两个复合的布局(网格和填充,如你所说)。

这是一些开始的代码。在显示它产生的图像之后有一个图像。 您还可以查看Eclipse的 SWT Layouts 视图。

Shell shell = new Shell();

FillLayout fillLayout = new FillLayout();
fillLayout.marginHeight = 5;
fillLayout.marginWidth = 5;
shell.setLayout( fillLayout );

Composite outer = new Composite( shell, SWT.BORDER );
outer.setBackground( new Color( null, 207, 255, 206 ) ); // Green

FormLayout formLayout = new FormLayout();
formLayout.marginHeight = 5;
formLayout.marginWidth = 5;
formLayout.spacing = 5;
outer.setLayout( formLayout );

Composite innerLeft = new Composite( outer, SWT.BORDER );
innerLeft.setLayout( new GridLayout() );
innerLeft.setBackground( new Color( null, 232, 223, 255 ) ); // Blue

FormData fData = new FormData();
fData.top = new FormAttachment( 0 );
fData.left = new FormAttachment( 0 );
fData.right = new FormAttachment( 10 ); // Locks on 10% of the view
fData.bottom = new FormAttachment( 100 );
innerLeft.setLayoutData( fData );

Composite innerRight = new Composite( outer, SWT.BORDER );
innerRight.setLayout( fillLayout );
innerRight.setBackground( new Color( null, 255, 235, 223 ) ); // Orange

fData = new FormData();
fData.top = new FormAttachment( 0 );
fData.left = new FormAttachment( innerLeft );
fData.right = new FormAttachment( 100 );
fData.bottom = new FormAttachment( 100 );
innerRight.setLayoutData( fData );

shell.open();

Code output

答案 1 :(得分:2)

您可以使用以下代码作为起点:

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

    SashForm form = new SashForm(shell, SWT.HORIZONTAL);
    form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Composite left = new Composite(form, SWT.BORDER);
    left.setLayout(new GridLayout(3, true));
    left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    for(int i = 0; i < 9; i++)
    {
        Button button = new Button(left, SWT.PUSH);
        button.setText("Button " + i);
        button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
    }

    final Composite right = new Composite(form, SWT.BORDER);
    right.setLayout(new GridLayout(1, true));
    right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Button fillButton = new Button(right, SWT.PUSH);
    fillButton.setText("Fill");
    fillButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    /* Set the width to 80% and 20% */
    form.setWeights(new int[] {4, 1});

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

看起来像这样:

enter image description here


它基本上是一个SashForm,有两个部分。左侧部分是GridLayout,有三列,右侧部分是GridLayout,有一列。无需混合Layout s。

百分比设置为form.setWeights(new int[] {4, 1});