复合标签左对齐,按钮右对齐
Composite with label aligned left and buttons right
我在 Eclipse 插件上工作,有时会显示一个弹出窗口。在弹出对话框中,我想创建一个区域,左侧有一个标签,右侧有两个按钮。
public void createBottom(Composite parent) {
Composite composite = new Composite(parent, SWT.FILL | SWT.WRAP | SWT.BORDER);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
composite.setLayoutData(gridData);
composite.setLayout(new GridLayout(3, false));
addLabel(composite);
addButton1(composite);
addButton2(composite);
}
目前的结果是这样的:
虽然我期待更像这样的东西:
如何对齐左边的 Label
和右边的两个按钮?
首先 SWT.FILL
和 SWT.WRAP
不是 Composite
的有效样式。控件的 Javadoc 指定了您可以使用的样式。
使用类似于:
Composite composite = new Composite(parent, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
composite.setLayoutData(gridData);
composite.setLayout(new GridLayout(3, false));
Label label = new Label(composite, SWT.BEGINNING);
label.setText("Test");
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button but1 = new Button(composite, SWT.PUSH);
but1.setText("OK");
but1.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
Button but2 = new Button(composite, SWT.PUSH);
but2.setText("Close");
but2.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
标签的布局数据在合成中抓取额外的space,并且按钮有结束对齐。
我在 Eclipse 插件上工作,有时会显示一个弹出窗口。在弹出对话框中,我想创建一个区域,左侧有一个标签,右侧有两个按钮。
public void createBottom(Composite parent) {
Composite composite = new Composite(parent, SWT.FILL | SWT.WRAP | SWT.BORDER);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
composite.setLayoutData(gridData);
composite.setLayout(new GridLayout(3, false));
addLabel(composite);
addButton1(composite);
addButton2(composite);
}
目前的结果是这样的:
虽然我期待更像这样的东西:
如何对齐左边的 Label
和右边的两个按钮?
首先 SWT.FILL
和 SWT.WRAP
不是 Composite
的有效样式。控件的 Javadoc 指定了您可以使用的样式。
使用类似于:
Composite composite = new Composite(parent, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
composite.setLayoutData(gridData);
composite.setLayout(new GridLayout(3, false));
Label label = new Label(composite, SWT.BEGINNING);
label.setText("Test");
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button but1 = new Button(composite, SWT.PUSH);
but1.setText("OK");
but1.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
Button but2 = new Button(composite, SWT.PUSH);
but2.setText("Close");
but2.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
标签的布局数据在合成中抓取额外的space,并且按钮有结束对齐。