我想在SWT中使用布局,其作用类似于Swing的cardLayout。 我的主要要求就像我有一个复选框并有2个SWT组。
并根据复选框选择和取消选择需要显示SWT 小组分别处于相同的位置。
根据复选框状态,一次只能看到一个组。 如何实现同样的目标。
答案 0 :(得分:8)
您可以使用StackLayout
来实现您的目标:
private static boolean buttonOnTop = true;
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Button switchButton = new Button(shell, SWT.NONE);
switchButton.setText("Switch");
final StackLayout layout = new StackLayout();
final Composite content = new Composite(shell, SWT.NONE);
content.setLayout(layout);
final Button button = new Button(content, SWT.PUSH);
button.setText("Button");
final Label label = new Label(content, SWT.NONE);
label.setText("Label");
layout.topControl = button;
switchButton.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
layout.topControl = (buttonOnTop) ? label : button;
content.layout();
buttonOnTop = !buttonOnTop;
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
通过设置StackLayout#topControl
,您可以将Control
“移动”到顶部。