[英]How to fill a ScrolledComposite?
我有一個從ScrolledComposite擴展的類,在這個類中,我創建了一個復合材料,用於填充父ScrolledComposite。 但每當我向內部復合材料添加標簽時,它只會包裹在右上角。 我希望我的內部Composite完全填充外部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);
}
});
}
}
只需在構造函數的末尾添加以下行:
this.setMinSize(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
this.setExpandHorizontal(true);
this.setExpandVertical(true);
筆記:
dispose()
你自己創建的所有資源( Color
, Font
,......) display.getSystemColor(SWT.COLOR_WHITE)
和display.getSystemFont()
來使用系統資源(無需處理display.getSystemFont()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.