[英]Make a column of a GridLayout fill horizontal
有一個給定的公式,例如您可以在圖像中看到的公式。 我正在為swt RCP-App編程。 我無法直接訪問組件,但必須使用布局將它們對齊。 首先,使用Gridlayout對齊組件。 第一列包含標簽,第二列包含文本字段。
剩下的就是,我希望文本字段具有相同的寬度。 我最初的計划是讓文本字段水平填充第二列,然后執行某項操作。 像是保證金權利。
GridData具有以下功能:
GridData data = new GridData(SWT.FILL, SWT.NONE, true, false);
data.horizontalAlignment = GridData.FILL;
...
我的GridLayout代碼如下所示:
GridLayout layout = new Gridlayout(2, false);
GridData data = new GridData(SWT.FILL, SWT.NONE, true, false);
data.horizontalAlignment = GridData.FILL;
parent.setLayout(layout); -> Produces the given image
parent.setLayoutData(data);
-> Seems to do nothing in combination with gridlayout, but the layout is needed
有更多種具有不同寬度的字段,但這足以說明問題。
有誰知道如何將布局與網格數據結合或有其他解決方案。 我想使第二列中的文本字段很漂亮,而無需直接使用它們。
它是這樣的:
謝謝你的幫助
GridData
必須應用於Text
,而不是其父級。 因此,您必須“觸摸”它們:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell();
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(2, false));
new Label(shell, SWT.NONE).setText("name");
Text name = new Text(shell, SWT.BORDER);
name.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
new Label(shell, SWT.NONE).setText("memory");
Text memory = new Text(shell, SWT.BORDER);
memory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
new Label(shell, SWT.NONE).setText("processors");
Text processors = new Text(shell, SWT.BORDER);
processors.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
看起來像這樣:
UPDATE
好的,再次考慮您的要求,並提出了另一個解決方案。 在此解決方案中,您不必具有對實際Text
對象的引用,但可以搜索它們:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell();
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(2, false));
new Label(shell, SWT.NONE).setText("name");
new Text(shell, SWT.BORDER);
new Label(shell, SWT.NONE).setText("memory");
new Text(shell, SWT.BORDER);
new Label(shell, SWT.NONE).setText("processors");
new Text(shell, SWT.BORDER);
applyGridData(shell);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
private static void applyGridData(Control parent)
{
if(parent instanceof Text)
{
parent.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
}
else if(parent instanceof Composite)
{
Composite comp = (Composite) parent;
for(Control control : comp.getChildren())
applyGridData(control);
}
}
基本上,此解決方案遞歸地在Composite
搜索Text
對象,然后將GridData
應用於它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.