【问题标题】:JFace resizing only the last group in a TitleAreaDialogJFace 仅调整 TitleAreaDialog 中最后一组的大小
【发布时间】:2013-01-29 08:51:29
【问题描述】:

下面附加的程序创建了一个 TitleAreaDialog,其中包含四个组。每个组都包含控件,最后一个是“动态的”,即在组合框中选择条目时,会生成一定数量的控件。

现在,幸运的是,对话框会自动调整大小 - 但我希望对调整大小有更多控制权。就像现在一样,对话框大小在组之间“分布”均匀。我想要完成的是放大(或缩小)最后一组,即使用户使用鼠标调整对话框的大小。三个静态组的大小(高度)应保持固定。

有没有办法做到这一点? 蒂亚!

public class DialogExample extends ApplicationWindow {
    class MyDialog extends TitleAreaDialog {
        MyDialog(Shell shell) {
            super(shell);
            setShellStyle(getShellStyle() | SWT.MAX | SWT.RESIZE);
        }

        private Composite createComposite(Composite parent) {
            Composite c = new Composite(parent, SWT.NONE);
            GridLayout layout = new GridLayout(1, false);
            layout.horizontalSpacing = 10;
            c.setLayout(layout);
            c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
            return c;
        }

        private Group createGroup(Composite parent, String title) {
            Group newGroup = new Group(parent, SWT.NONE);
            newGroup.setText(title);
            newGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
            GridLayout layout = new GridLayout();
            newGroup.setLayout(layout);          
            return newGroup;
        }

        private Group createGroup(Composite parent, int controlCount) {
            Group newGroup = createGroup(parent, "Group " + controlCount);
            for (int i = 0; i < controlCount; i++) {
                Label l = new Label(newGroup, SWT.NONE);
                l.setText("Label " + (i + 1));
            }          
            return newGroup;
        }

        private Group createControlGroup(final Composite parent) {          
            final Group newGroup = createGroup(parent, "Control Group");
            final Combo combo = new Combo(newGroup, SWT.READ_ONLY);
            combo.add("1");
            combo.add("2");
            combo.add("3");
            combo.addSelectionListener(new SelectionListener() {
                  @Override
                  public void widgetSelected(SelectionEvent event) {
                      for (Control c: newGroup.getChildren()) {
                           if (c != combo) {
                                c.dispose();
                           }
                      }
                      for (int i = 0; i <combo.getSelectionIndex() + 1; i++) {
                          Label l = new Label(newGroup, SWT.None);
                          l.setText("Dynamic Label " + (i + 1));
                      }
                      parent.getShell().pack();
                  }

                  @Override
                  public void widgetDefaultSelected(SelectionEvent arg0) {
                  }
            });
        return newGroup;
     }

     public void create() {
        super.create();
        setTitle("Title");
        setMessage("Message");
     }

     protected Control createDialogArea(Composite parent) {
        Composite main = createComposite(parent);
        createGroup(main, 1);
        createGroup(main, 2);
        createGroup(main, 3);     
        createControlGroup(main);
        return main;
      }
    }

    public DialogExample() 
    {
      super(null);
    }

    public void run() {
      setBlockOnOpen(true);
      open();
      Display.getCurrent().dispose();
    }

    protected Control createContents(Composite parent) {
      Label label = new Label(parent, SWT.CENTER);
      label.setText("Dialog example");
      TitleAreaDialog dialog = new MyDialog(parent.getShell());

      dialog.open();
      return label;
    }

    public static void main(String... args) {
      new DialogExample().run();
    }
}

【问题讨论】:

    标签: java resize swt jface titleareadialog


    【解决方案1】:

    原因是您使用了以下内容:

    newGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    

    这意味着每个组都应该跨越父组并占据尽可能多的空间。

    您可以使用以下方法解决此问题:

    newGroup.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
    

    对于不应垂直拉伸的组件和:

    newGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    

    对于那些应该的人。


    如果您还没有阅读“理解 SWT 中的布局”,请阅读 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-02
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      相关资源
      最近更新 更多