2013-07-24 32 views
1

我有一个从ScrolledComposite扩展的类,在这个类内我创建了一个用于填充父ScrolledComposite的Composite。但是,无论何时将标签添加到内部复合材料中,它都仅包含在右上角。我想要我的内部复合材料完全填充外部ScrolledComposite。有没有办法做到这一点?如何填写ScrolledComposite?

public class Container extends ScrolledComposite { 

    final Composite comp; 
    Label nameLabel, sizeLabel, dateLabel; 
    ArrayList<Label> entrySize, date; 
    ArrayList<SimpleEntry> entry; 
    Color white; 
    Font font; 

    public Container(Composite parent, int style) { 
     super(parent, style); 
     white = new Color(getDisplay(), 255, 255, 255); 
     font = new Font(getDisplay(), "Arial", 16, SWT.BOLD); 
     setBackground(white); 
     setLayout(new GridLayout(1, false)); 

     comp = new Composite(this, SWT.BORDER); 
     GridData compData = new GridData(SWT.FILL, SWT.FILL, true, true); 
     comp.setLayoutData(compData); 
     setContent(comp); 
     comp.setBackground(white); 
     comp.setLayout(new GridLayout(3, false)); 

     nameLabel = new Label(comp, SWT.BOLD); 
     sizeLabel = new Label(comp, SWT.BOLD); 
     dateLabel = new Label(comp, SWT.BOLD); 

     GridData d1 = new GridData(SWT.CENTER, SWT.CENTER, true, false); 
     nameLabel.setLayoutData(d1); 

     GridData d2 = new GridData(SWT.CENTER, SWT.CENTER, true, false); 
     sizeLabel.setLayoutData(d2); 

     GridData d3 = new GridData(SWT.CENTER, SWT.CENTER, true, false); 
     dateLabel.setLayoutData(d3); 

     nameLabel.setBackground(white); 
     sizeLabel.setBackground(white); 
     dateLabel.setBackground(white); 

     nameLabel.setText("Name"); 
     sizeLabel.setText("Size"); 
     dateLabel.setText("Last Modified"); 

     nameLabel.setFont(font); 
     sizeLabel.setFont(font); 
     dateLabel.setFont(font); 

     comp.setSize(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 
     comp.layout(); 

     addDisposeListener(new DisposeListener() { 
      @Override 
      public void widgetDisposed(DisposeEvent e) { 
       Container.this.widgetDisposed(e); 
      } 
     }); 
    } 
} 
+1

需要代码.... – Baz

+0

我已经编辑了@Baz – Anas

回答

3

就在构造函数的末尾添加这些行:

this.setMinSize(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 
this.setExpandHorizontal(true); 
this.setExpandVertical(true); 

注:

  • 记住dispose()自己创建的所有资源(ColorFont。 ..)
  • 考虑我们通过使用display.getSystemColor(SWT.COLOR_WHITE)display.getSystemFont(),系统资源(无需处置)。
+0

谢谢@Baz。是的,我已经把它们放置在这里没有显示的单独功能中。 – Anas

+0

我以为它会做到这一点,因为它填充了父项,但填充数据后,我来了解父母现在不滚动:/ – Anas

+0

@Anas如果在添加内容后再次设置最小大小,会发生什么情况? – Baz