问答
我需要添加一个 Gridlayout 来 createTopButtons 吗?
不,您需要将按钮放在另一个组合中,并且这个子组合应该有一个包含两列的网格布局。
同样在 createDialogArea 中,我可以使用
网格布局?示例 - 我希望 createTopButtons 位于左上角,然后我希望 createTableViewer 居中
当然,为什么不呢。但是您还需要一个名为GridData 的东西并将其设置为area.setLayoutData(gridData);。为了使某些东西居中,您的 griddata 可能看起来像这样gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false);
您是否使用布局内的布局来排列您的项目/按钮/标签
您正在创建的复合材料,如 createTopButtons。然后在中使用布局
createDialogArea 来排列方法创建的组合?
是的。虽然我勉强得到下半场 composite created by the methods
代码
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class SideBySide {
public static void main(String[] args) {
new SideBySide().start();
}
private Shell shell;
public void start()
{
Display display = new Display();
shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
shell.setLayoutData(gridData);
shell.setText("Side By Side");
createDialogArea(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
protected Control createDialogArea(Composite parent)
{
final Composite area = new Composite(parent, SWT.NONE);
final GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 15;
gridLayout.marginHeight = 10;
area.setLayout(gridLayout);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
shell.setLayoutData(gridData);
area.setLayoutData(gridData);
createTopButtons(area);
createTableViewer(area);
return area;
}
private void createTableViewer(Composite area)
{
Table table = new Table(area, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(gridData);
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableColumn column = new TableColumn(table, SWT.LEFT);
column.setWidth(320);
column.setText("Column 1");
column = new TableColumn(table, SWT.LEFT);
column.setWidth(320);
column.setText("Column 2");
}
protected void createTopButtons(Composite parent)
{
Composite composite = new Composite(parent, SWT.NONE);
GridLayout gridLayout = new GridLayout(2, false);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 0;
gridLayout.horizontalSpacing = 0;
composite.setLayout(gridLayout);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
composite.setLayoutData(gridData);
gridData = new GridData(SWT.DEFAULT, SWT.FILL, false, false);
Button pdfButton = new Button(composite, SWT.PUSH);
pdfButton.setText("Create PDF");
pdfButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
pdfButton.setLayoutData(gridData);
Button plotButton = new Button(composite, SWT.PUSH);
plotButton.setText("Plot");
plotButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
plotButton.setLayoutData(gridData);
}
}
代码输出
进一步阅读
这是一个great article,用于了解 SWT 中的布局。