我正在处理这个“Composite”画布,其他的“Composite”可以被添加和移除。
我对整个布局概念的理解还不够清晰。
当子级被添加到容器中时,考虑到容器有一个填充父级的“GridData”,难道父级也不知道子级已经调整大小了吗?子级在其容器布局完成后仍然隐藏,因为有一个shell(顶级父级)。
如果问题不太清楚,请随时要求更多细节。同时,请尽量不要指向“了解SWT中的布局”文章。
我对整个布局概念的理解还不够清晰。
当子级被添加到容器中时,考虑到容器有一个填充父级的“GridData”,难道父级也不知道子级已经调整大小了吗?子级在其容器布局完成后仍然隐藏,因为有一个shell(顶级父级)。
如果问题不太清楚,请随时要求更多细节。同时,请尽量不要指向“了解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++) );
}
}
有趣的事实:
这个问题非常相关,与另一个问题非常相似,该问题是9分钟前发布的。有人要么是通灵,要么是在跟踪我。
shell.setSize(Point)
(它基本上是pack(true)
)。在我完成整个实现后,我会回来告诉你们的。 - Georgianpack(true)
执行的与我上面两行代码相同的操作。从中提取出来的代码还检查了最小大小,这是pack
无法完成的。 - greg-449