[英]How to make child composite fill parent in swt
這是我的代碼:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Composite childComposite = new Composite(shell, SWT.BORDER);
childComposite.setLayout(new GridLayout(2, false));
Text text1 = new Text(childComposite, SWT.NONE);
Text text2 = new Text(childComposite, SWT.NONE );
Label label = new Label(shell, SWT.NONE);
label.setText("Very loooooooooooooooooooooooong text");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
這會產生這樣的東西:
我的問題是如何讓我的子復合水平填充父級(至少與下面標簽的寬度相同)。 我試過用這樣的東西:
Composite childComposite = new Composite(shell, SWT.BORDER | SWT.FILL);
......但這並沒有改變任何事情。
此外,當子節點與父節點寬度相同時,我希望文本小部件也填充它們所在的復合,但寬度不同 - 例如,第一個是20%,第二個是80%。 我應該檢查哪些可能性來實現這一目標?
使用GridLayout
,使用GridData
參數來控制setLayoutData
方法以指定網格的填充方式。
你想要的東西:
shell.setLayout(new GridLayout(1, false));
Composite childComposite = new Composite(shell, SWT.BORDER);
// Composite fills the grid row
childComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
// Use 5 equal sized columns
childComposite.setLayout(new GridLayout(5, true));
// First text fills the first column
Text text1 = new Text(childComposite, SWT.NONE);
text1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
// Second text fills the next 4 columns
Text text2 = new Text(childComposite, SWT.NONE);
text2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 4, 1));
Label label = new Label(shell, SWT.NONE);
label.setText("Very loooooooooooooooooooooooong text");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.