问题
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:
Right, here you go: Use the GridData#widthHint
values to force a certain width of the Composite
s. 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:
After resizing:
来源:https://stackoverflow.com/questions/19348681/setting-the-width-of-the-gridlayout-columns