解决该问题的方法是设置
ScrolledComposite每次添加/删除窗口小部件的最小大小。您将从中使用
setMinSize(Point)API
ScrolledComposite。另外,请记住
layout()滚动复合内容,而不是其子元素。放置孩子将自动完成。
当前, 添加 按钮与每个新添加的组件一起移动。如果您希望它不移动,只需将其移动到滚动的复合内容之外。
另外,我还整理了一下代码,希望您不要介意。:-)
public class DynamicDialog extends Dialog { private int count = 1; public DynamicDialog(final Shell parent) { super(parent); } @Override protected Control createDialogArea(final Composite parent) { final Composite container = (Composite) super.createDialogArea(parent); container.setLayout(new FillLayout()); final ScrolledComposite scrolledComposite = new ScrolledComposite(container, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); scrolledComposite.setExpandHorizontal(true); scrolledComposite.setExpandVertical(true); final Composite composite = new Composite(scrolledComposite, SWT.NONE); composite.setLayout(new GridLayout(1, false)); scrolledComposite.setContent(composite); scrolledComposite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); final Composite composite_1 = new Composite(composite, SWT.NONE); composite_1.setLayout(new GridLayout(2, false)); composite_1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); final Label lblDefault = new Label(composite_1, SWT.NONE); lblDefault.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); lblDefault.setText("Default:"); final Combo combo = new Combo(composite_1, SWT.NONE); combo.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1)); final Composite composite_2 = new Composite(composite, SWT.NONE); composite_2.setLayout(new GridLayout(4, false)); composite_2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); final Composite composite_3 = new Composite(composite, SWT.NONE); composite_3.setLayout(new GridLayout()); composite_3.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); final Button btnAdd = new Button(composite_3, SWT.NONE); btnAdd.setText("Add"); btnAdd.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(final SelectionEvent e) { final Label label2 = new Label(composite_2, SWT.NONE); label2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); label2.setText( String.valueOf(count++) ); new Text(composite_2, SWT.BORDER).setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); new Text(composite_2, SWT.BORDER).setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); new Button(composite_2, SWT.NONE).setText("Delete"); // DO THIS: scrolledComposite.layout(true, true); scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); // .. and it will work } }); return container; } @Override protected void createButtonsForButtonBar(final Composite parent) { createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); } @Override protected Point getInitialSize() { return new Point(450, 300); } public static void main(final String[] args) { final Shell shell = new Shell(new Display()); shell.setLayout(new FillLayout()); new DynamicDialog(shell).open(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)