📄 stacklayoutsample.java
字号:
package rcpbook.swtjface.sample;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class StackLayoutSample {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(240, 180);
shell.setText("StackLayout布局");
shell.setLayout(new FillLayout());
final Composite composite1 = new Composite(shell, SWT.NONE);
final StackLayout stackLayout = new StackLayout();
composite1.setLayout(stackLayout);
final Composite c1 = createStack1(composite1);
final Composite c2 = createStack2(composite1);
stackLayout.topControl = c1;
final Composite composite2 = new Composite(shell, SWT.NONE);
composite2.setLayout(new FillLayout(SWT.VERTICAL));
final Button button1 = new Button(composite2, SWT.NONE);
button1.setText("显示堆栈页面1");
button1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
stackLayout.topControl = c1;
composite1.layout();
}
});
final Button button2 = new Button(composite2, SWT.NONE);
button2.setText("显示堆栈页面2");
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
stackLayout.topControl = c2;
composite1.layout();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
// 堆栈页面1的内容
public static Composite createStack1(Composite parent) {
Composite composite = new Composite(parent, SWT.BORDER);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.marginTop = 10;
layout.marginBottom = 10;
layout.marginRight = 10;
layout.horizontalSpacing = 10;
layout.makeColumnsEqualWidth = true;
composite.setLayout(layout);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.widthHint = 60;
gridData.heightHint = 100;
gridData.verticalSpan = 2;
Button button1 = new Button(composite, SWT.NONE);
button1.setText("按钮1");
button1.setLayoutData(gridData);
gridData = new GridData(GridData.FILL_BOTH);
Button button2 = new Button(composite, SWT.NONE);
button2.setText("按钮2");
button2.setLayoutData(gridData);
gridData = new GridData(GridData.FILL_BOTH);
Button button3 = new Button(composite, SWT.NONE);
button3.setText("按钮3");
button3.setLayoutData(gridData);
return composite;
}
// 堆栈页面2的内容
public static Composite createStack2(Composite parent) {
Composite composite = new Composite(parent, SWT.BORDER);
RowLayout rowLayout = new RowLayout();
rowLayout.marginTop = 5;
rowLayout.spacing = 5;
composite.setLayout(rowLayout);
Button button1 = new Button(composite, SWT.NONE);
button1.setText("按钮1");
button1.setLayoutData(new RowData(80, 40));
Button button2 = new Button(composite, SWT.NONE);
button2.setText("按钮2");
button2.setLayoutData(new RowData(new Point(90, 40)));
new Button(composite, SWT.NONE).setText("按钮3");
new Button(composite, SWT.NONE).setText("按钮4");
new Button(composite, SWT.NONE).setText("按钮5");
return composite;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -