1

我已经使用 SWT 几年了,但我似乎无法弄清楚这一点:

我需要将 VIEW 分成 2 个“区域”:左和右所以我使用带有 GridLayout 的 Composite 和 2 列来做到这一点。

在 RIGHT 复合内,我有固定数量的列(在另一个 GridLayout 复合内创建),但在 LEFT 复合内,我需要创建跨越复合限制的动态列数......

有没有办法做到这一点?

我在左合成中尝试了 RowLayout,但是这两个不匹配:-\

谢谢

4

1 回答 1

2

因为不清楚你的意思是什么

  1. 跨越复合限制的列
  2. 我在左合成中尝试了 RowLayout,但是这两个不匹配

因此,我假设您想要为您的左侧合成动态更改网格布局。请参阅下面的代码,特别是button.addSelectionListener().

import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class DynamicComposite 
{
    public static void main(String[] args) {
        new DynamicComposite().start();
    }

    private Composite compositeLeft;
    private Composite compositeRight;
    private Random rand;

    public void start()
    {
        rand = new Random(System.currentTimeMillis()); 

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(2, true));
        shell.setText("Dynamic Columns");

        createRightComposite(shell);
        createLeftComposite(shell);
        createButtonComposite(shell);

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

    private void createButtonComposite(Shell shell) 
    {
        Label l = new Label(shell, SWT.SEPARATOR|SWT.HORIZONTAL);
        GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
        gridData.horizontalSpan = 2;
        l.setLayoutData(gridData);

        Button button = new Button(shell, SWT.PUSH);
        gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false);
        gridData.horizontalSpan = 2;
        button.setLayoutData(gridData);
        button.setText("Change Columns !!");

        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                if(compositeLeft == null || compositeLeft.isDisposed())
                    return;

                GridLayout layout = (GridLayout)compositeLeft.getLayout();
                int col = rand.nextInt(7);
                if(col == 0)
                    col = 1;
                layout.numColumns = col;
                //compositeLeft.setLayout(layout);
                compositeLeft.layout(); // You need to re-layout your composite
            }
        });
    }

    private void createRightComposite(Shell shell) 
    {
        compositeLeft = new Composite(shell, SWT.NONE);
        GridLayout gridLayout = new GridLayout(3, true);
        gridLayout.marginWidth = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginRight= 0;
        gridLayout.marginTop = 0;
        gridLayout.marginBottom = 0;
        gridLayout.horizontalSpacing = 0;

        gridLayout.verticalSpacing = 0;
        gridLayout.horizontalSpacing = 0;
        compositeLeft.setLayout(gridLayout);
        GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        compositeLeft.setLayoutData(gridData);

        int counter = 17;
        for(int i=0; i<counter; i++)
        {
            Button button = new Button(compositeLeft, SWT.PUSH);
            button.setText("Button " + (i+1));
            GridData bData = new GridData(SWT.FILL, SWT.FILL, true, false);
            button.setLayoutData(bData);
        }
    }

    private void createLeftComposite(Shell shell) 
    {
        compositeRight = new Composite(shell, SWT.NONE);
        GridLayout gridLayout = new GridLayout(3, true);
        gridLayout.marginWidth = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginRight= 0;
        gridLayout.marginTop = 0;
        gridLayout.marginBottom = 0;
        gridLayout.horizontalSpacing = 0;
        compositeRight.setLayout(gridLayout);

        GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        compositeRight.setLayoutData(gridData);

        int counter = 7;
        for(int i=0; i<counter; i++)
        {
            Button button = new Button(compositeRight, SWT.PUSH);
            button.setText("Button " + (i+1));
            GridData bData = new GridData(SWT.FILL, SWT.FILL, true, false);
            button.setLayoutData(bData);
        }
    }
}
于 2012-08-29T05:06:52.553 回答