22:46:21package sunnie.swt; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.custom.StackLayout; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.graphics.Point; public class Dynamic { private StackLayout stackLayout = new StackLayout(); private Composite infoComp; private Composite commComp; private List selectList; private Composite rightComp; public static void main(String[] args) { new Dynamic().open(); } public void open() { Display display = Display.getDefault(); Shell shell = new Shell(); shell.setSize(550, 350); shell.setText("个人设置"); shell.setLayout(new GridLayout()); shell.setSize(new Point(461, 295)); SashForm sashForm = new SashForm(shell, SWT.BORDER); sashForm.setLayoutData(new GridData(GridData.FILL_BOTH)); selectList = new List(sashForm, SWT.BORDER); selectList.setItems(new String[] { "个人资料", "联系方式" }); selectList.addSelectionListener(new MySelectionListener()); rightComp = new Composite(sashForm, SWT.NONE); rightComp.setLayout(stackLayout); infoComp = createYourDataComp(rightComp); commComp = createOtherComp(rightComp); stackLayout.topControl = infoComp; sashForm.setWeights(new int[] { 1, 4 }); Composite buttonComp = new Composite(shell, SWT.BORDER); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.END; buttonComp.setLayoutData(gridData); RowLayout rowLayout = new RowLayout(); rowLayout.spacing = 15; buttonComp.setLayout(rowLayout); new Button(buttonComp, SWT.NONE).setText(" 确定 "); new Button(buttonComp, SWT.NONE).setText(" 取消 "); new Button(buttonComp, SWT.NONE).setText(" 应用 "); shell.layout(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } private Composite createYourDataComp(Composite rightComp) { Composite comp = new Composite(rightComp, SWT.NONE); comp.setLayout(new GridLayout(4, false)); new Label(comp, SWT.NONE).setText("用户名:"); Text numberText = new Text(comp, SWT.BORDER); numberText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); new Label(comp, SWT.NONE).setText("性别:"); Combo genderCombo = new Combo(comp, SWT.NONE); genderCombo.add("男"); genderCombo.add("女"); genderCombo.select(0); genderCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); new Label(comp, SWT.NONE).setText("姓名:"); Text nameText = new Text(comp, SWT.BORDER); nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); new Label(comp, SWT.NONE).setText("年龄:"); Text oldText = new Text(comp, SWT.BORDER); oldText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); return comp; } private Composite createOtherComp(Composite rightComp) { Composite comp = new Composite(rightComp, SWT.NONE); comp.setLayout(new GridLayout(2, false)); new Label(comp, SWT.NONE).setText("联系地址"); Text addrText = new Text(comp, SWT.BORDER); addrText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); new Label(comp, SWT.NONE).setText("联系电话"); Text telText = new Text(comp, SWT.BORDER); telText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); return comp; } private class MySelectionListener extends SelectionAdapter { public void widgetSelected(SelectionEvent e) { if (selectList.getSelectionIndex() == 0) stackLayout.topControl = infoComp; else stackLayout.topControl = commComp; rightComp.layout(); } } }