// 建立第二个按钮
data = new GridData(GridData.FILL_BOTH);
Button two = new Button(composite, SWT.PUSH);
two.setText("按钮2");
two.setLayoutData(data);
// 建立第三个按钮
data = new GridData(GridData.HORIZONTAL_ALIGN_END);
Button three = new Button(composite, SWT.PUSH);
three.setText("按钮3");
three.setLayoutData(data);
// 建立第四个按钮
data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
Button four = new Button(composite, SWT.PUSH);
four.setText("按钮4");
four.setLayoutData(data);
// 建立下面的一个长按钮
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
data.horizontalSpan = 3;
data.verticalSpan = 2;
data.heightHint = 150;
Button five = new Button(shell, SWT.PUSH);
five.setText("按钮5");
five.setLayoutData(data);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
}
Button button = new Button(shell, SWT.PUSH);
button.setText("按钮");
FormData data = new FormData();
data.left = new FormAttachment(40);
button.setLayoutData(data);
Button button = new Button(shell, SWT.PUSH);
button.setText("按钮");
FormData data = new FormData();
data.left = new FormAttachment(40);
data.right = new FormAttachment(60);
button.setLayoutData(data);
public class TestStackLayout
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
StackLayout layout = new StackLayout();
shell.setLayout(layout);
StackLayoutSelectionAdapter adapter = new StackLayoutSelectionAdapter(shell, layout);
Button one = new Button(shell, SWT.PUSH);
one.setText("按钮1");
one.addSelectionListener(adapter);
Button two = new Button(shell, SWT.PUSH);
two.setText("按钮2");
two.addSelectionListener(adapter);
Button three = new Button(shell, SWT.PUSH);
three.setText("按钮3");
three.addSelectionListener(adapter);
layout.topControl = one;
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
}
class StackLayoutSelectionAdapter extends SelectionAdapter
{
Shell shell;
StackLayout layout;
public void widgetSelected(SelectionEvent event)
{
Control control = layout.topControl;
Control[] children = shell.getChildren();
int i = 0;
for (int n = children.length; i < n; i++)
{
Control child = children;
if (child == control)
{
break;
}
}
++i;
if (i >= children.length)
i = 0;
layout.topControl = children;
shell.layout();
}
}