1

私は数年間SWTを使用してきましたが、これを理解できないようです:

ビューを2つの「ゾーン」に分割する必要があります:LEFTとRIGHTだから、2列のGridLayoutでコンポジットを使用してそれを行います。

RIGHT コンポジット内には FIXED 数の列 (別の GridLayout コンポジット内で作成) がありますが、LEFT コンポジット内では、コンポジット制限にまたがる動的数の列を作成する必要があります...

これを達成する方法はありますか?

左のコンポジット内でRowLayoutを試しましたが、これら2つは一致しません:-\

ありがとう

4

1 に答える 1

2

言ってる意味が不明なので

  1. 複合制限にまたがる列
  2. 左のコンポジット内で RowLayout を試しましたが、これら 2 つが一致しません

したがって、左側のコンポジットに対して動的に変化するグリッドレイアウトが必要であると想定しています。以下のコード、特に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 に答える