gpt4 book ai didi

java - 设置 GridLayout 列的宽度

转载 作者:行者123 更新时间:2023-11-30 06:22:23 26 4
gpt4 key购买 nike

我在 Composite 中有一个 GridLayout,其中有两列。我希望列宽为 Shell 宽度的 75 % 和 25 % 。怎么做?

最佳答案

好的,开始吧:使用 GridData#widthHint 值来强制 Composite 的特定宽度。根据 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();
}

开始后:

enter image description here

调整大小后:

enter image description here

关于java - 设置 GridLayout 列的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19348681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com