我正在研究此Composite
畫布,其他Composite
可能會被添加和刪除。SWT Shell根據兒童調整大小
我對整個佈局概念如何仍然迷霧的理解。
當孩子們被添加到容器中時,鑑於容器有填充父母的GridData
這一事實,父母是否也應該知道孩子調整了大小?由於殼(頂級父母),兒童在放好容器後仍然隱藏起來。
如果問題太模糊,請不要猶豫,以瞭解更多詳情。另外,請儘量不要將我指向瞭解SWT文章中的佈局。
/**
*
* @author ggrec
*
*/
public class SSCCE
{
// ==================== 2. Instance Fields ============================
private Composite componentContainer;
private int componentCount = 0;
// ==================== 3. Static Methods =============================
public static void main(final String[] args)
{
new SSCCE();
}
// ==================== 4. Constructors ===============================
private SSCCE()
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
createContents(shell);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
// ==================== 5. Creators ===================================
private void createContents(final Composite parent)
{
parent.setLayout(new GridLayout());
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Button button = new Button(parent, SWT.PUSH);
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
button.setText("Add New Component");
button.addSelectionListener(new SelectionAdapter()
{
@Override public void widgetSelected(final SelectionEvent e)
{
addNewComponent();
componentContainer.layout();
}
});
componentContainer = new Composite(parent, SWT.BORDER);
componentContainer.setLayout(new GridLayout());
componentContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
}
// ==================== 6. Action Methods =============================
private void addNewComponent()
{
final Composite component = new Composite(componentContainer, SWT.BORDER);
component.setLayout(new FillLayout());
component.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
final Label label = new Label(component, SWT.NONE);
label.setText(String.valueOf(componentCount++));
}
}
有趣的事實: 這個問題是veeeery相關to this other one, that was posted 9 minutes ago。有人要麼通靈,要麼跟蹤我。
您的解決方案似乎適用於更復雜的示例。我想知道'shell.setSize(Point)'是否在我的情況下是強制的(它基本上是'pack(true)')。在完成整個事情之後,我會盡快回復你們。 – GGrec
'pack(true)'與我最後兩行完全相同。我從中提取的代碼也會檢查無法用'pack'完成的最小大小。 –
@ greg-449對於JFace組合框是一樣的。我問過一個問題[這裏](http://stackoverflow.com/questions/32561924/how-to-truncate-a-long-name-in-a-jface-combobox)。 –