设置GridLayout列的宽度。

时间:2023-01-12 15:27:07

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?

我在一个合成中有一个GridLayout,里面有两个列。我想让列宽度75%和25%的壳宽度。如何做呢?

1 个解决方案

#1


5  

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:

对,在这里:使用GridData#widthHint值来强制复合材料的一定宽度。根据外壳的宽度计算宽度:

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:

后开始:

设置GridLayout列的宽度。

After resizing:

调整后:

设置GridLayout列的宽度。

#1


5  

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:

对,在这里:使用GridData#widthHint值来强制复合材料的一定宽度。根据外壳的宽度计算宽度:

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:

后开始:

设置GridLayout列的宽度。

After resizing:

调整后:

设置GridLayout列的宽度。