java - SWT - GridData 和按钮

标签 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;
}

这里是按钮方法。

 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();
     }
   });  
 }

我需要为 createTopButtons 添加一个 Gridlayout 吗?
如果是这样,我如何将它们并排放置而不是一个放在另一个之上。

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

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

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

最佳答案

问答

Would I need to add a Gridlayout to createTopButtons?

不,您需要将按钮放在另一个组合中,这个子组合应该有一个包含两列的网格布局。

Also in the createDialogArea, can I arrange my components using the gridLayout? Example - I want createTopButtons to be on the top left hand side, then I want createTableViewer centered

当然,为什么不呢。但是您还需要一个名为 GridData 的东西,并将其设置为 area.setLayoutData(gridData);。为了使某物居中,您的网格数据可能如下所示 gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false);

Do you arrange your items/buttons/labels using a layout inside the composite you are creating like createTopButtons. Then use a layout in the createDialogArea to arrange the composite created by the methods?

是的。尽管我无法得到后半部分 由方法创建的复合 Material

代码

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);
    }
}

代码输出

enter image description here

进一步阅读

这是一个 great article了解 SWT 中的布局。

关于java - SWT - GridData 和按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12170017/

相关文章:

r - 如何在R中结合使用pairs函数和布局?

java - 如何在SWT中一次监听按键和鼠标事件(Shift+Left MouseUp)?

java - JSP, "User cannot be resolved to a type"

java - 我可以用 Map<String, Integer> 覆盖 Map<Integer, Integer> 吗?

android - 为 TextView 使用 XML 布局

Android - 禁用可扩展列表中一组的 onClick 突出显示?

Java SWT 图像背景颜色

java - swt 库 - 链接到 eclipse javadoc

java - 如何在圆形 ImageView 中添加图标

java - Android - ViewPager 不显示 fragment