设置GridLayout列的宽度 [英] Setting the width of the GridLayout columns

查看:1763
本文介绍了设置GridLayout列的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridLayout 在一个复合中,我里面有两列。我希望列宽为$ code> Shell的宽度为75%和25%。怎么做?

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?

推荐答案

对,这里你去:使用 GridData#widthHint 强制某个宽度的复合 s。根据 Shell的宽度计算宽度

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();
}

开始后:

调整大小后:

这篇关于设置GridLayout列的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆