Setting the width of the GridLayout columns

2019-07-21 20:22发布

I have a GridLayout inside a Composite and I have two column inside that. I want to have column width 75 % and 25 % of the Shell width . How to do that?

1条回答
Luminary・发光体
2楼-- · 2019-07-21 21:01

Right, here you go: Use the GridData#widthHint values to force a certain width of the Composites. Compute the width based on the width of the Shell:

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

    Composite left = new Composite(shell, SWT.BORDER);
    Composite right = new Composite(shell, SWT.BORDER);

    final GridData leftData = new GridData(SWT.FILL, SWT.FILL, true, true);
    final GridData rightData = new GridData(SWT.FILL, SWT.FILL, true, true);

    left.setLayoutData(leftData);
    right.setLayoutData(rightData);

    shell.addListener(SWT.Resize, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            Point size = shell.getSize();

            leftData.widthHint = (int) (size.x * 0.75);
            rightData.widthHint = size.x - leftData.widthHint;

            System.out.println(leftData.widthHint + " + " + rightData.widthHint + " = " + size.x);
        }
    });

    shell.pack();
    shell.open();
    shell.layout();

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

After start:

enter image description here

After resizing:

enter image description here

查看更多
登录 后发表回答