SWT - GridData和按钮

jkt*_*ter 2 java layout swt

我有一个方法来创建2个按钮.这些按钮与"确定","关闭"按钮不同.单击时,它们将执行不同的操作.我希望2个按钮并排放在我的基础复合材料的顶部.哪个使用GridLayout.我希望能够将按钮并排放置.

这是我添加方法的createDialogArea.

  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);
  createTopButtons(area);
  createTableViewer(area);
  return area;
}
Run Code Online (Sandbox Code Playgroud)

这是按钮方法.

 protected void createTopButtons(Composite parent) {
   Button pdfButton = new Button(parent, SWT.PUSH);
   pdfButton.setText("Create PDF");
   pdfButton.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent e) {
        close();
     }
   }); 

   Button plotButton = new Button(parent, SWT.PUSH);
   plotButton.setText("Plot");
   plotButton.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent e) {
        close();
     }
   });  
 }
Run Code Online (Sandbox Code Playgroud)

我需要将Gridlayout添加到createTopButtons吗?
如果是这样的话,我怎么能并排得到它们而不是另一个呢?

同样在createDialogArea中,我可以使用gridLayout安排我的组件吗?

示例 - 我希望createTopButtons位于左上角,然后我希望createTableViewer居中

您是否使用您正在创建的合成内部布局(如createTopButtons)来排列项目/按钮/标签.然后使用createDialogArea中的布局来排列由方法创建的复合?

Fav*_*ius 6

问答

我需要将Gridlayout添加到createTopButtons吗?

不需要将按钮放在另一个复合材料中,这个子复合材料应该有一个包含两列的gridlayout.

同样在createDialogArea中,我可以使用gridLayout安排我的组件吗?示例 - 我希望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);
    }
}
Run Code Online (Sandbox Code Playgroud)

代码输出

在此输入图像描述

进一步阅读

这是一篇了解SWT中布局的好文章.

  • 你为我解答了很多问题。非常感谢您展示代码作为示例。遵循一个例子要容易得多。 (2认同)