【问题标题】:SWT - GridData and buttonsSWT - GridData 和按钮
【发布时间】:2012-08-29 02:11:51
【问题描述】:

我有一种方法可以创建 2 个按钮。这些按钮不同于 OK、Close 按钮​​。单击时,它们将执行不同的操作。我希望 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();
     }
   });  
 }

是否需要添加 Gridlayout 来创建顶部按钮?
如果是这样,我将如何让它们并排而不是一个在另一个之上。

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

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

您是否使用您正在创建的复合材料中的布局来排列您的项目/按钮/标签,例如 createTopButtons。然后在 createDialogArea 中使用一个布局来排列方法创建的组合?

【问题讨论】:

    标签: java layout swt


    【解决方案1】:

    问答

    我需要添加一个 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 中的布局。

    【讨论】:

    • 你为我回答了很多问题。非常感谢您展示代码作为示例。跟随一个例子要容易得多。
    • 所有问题都应该这样回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2013-12-23
    • 2011-01-08
    • 2013-04-13
    相关资源
    最近更新 更多