使用外壳和对齐按钮调整表格大小?

时间:2018-06-29 13:11:53

标签: java eclipse button swt

如果我拖动外壳的边框,有人可以告诉我如何调整表格大小。换句话说,我希望将表末端连接到外壳,以便在调整外壳大小时可以使用表。我也有按钮的问题。我需要将它们放置在桌子的右下角,但要设置对齐的内容(GridData.HORIZONTAL_ALIGN_BEGINNING或新的GridData(SWT.END,SWT.END,false,false)或新的GridData(SWT.END), SWT.BOTTOM,false,false)与输出完全相同)它在左下角设置了两个按钮,在右下角设置了一个按钮。这是表格的代码和图片:

try {
        display = new Display();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    shell = new Shell(display);
    shell.setLayout(new GridLayout());

    GridLayout layout = new GridLayout();
    shell.setLayout(layout);

    Composite composite = new Composite(shell, SWT.NONE);
    // Create a composite to hold the children
    GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_BOTH);
    composite.setLayoutData(gridData);

    // Set numColumns to 3 for the buttons
    GridLayout layout1 = new GridLayout(3, false);
    layout1.marginWidth = 4;
    composite.setLayout(layout1);

    int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | 
            SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
    final int NUMBER_COLUMNS = 6;
    table = new Table(composite, style);

    GridData gridData1 = new GridData(GridData.FILL_BOTH);
    gridData1.grabExcessVerticalSpace = true;
    gridData1.horizontalSpan = 3;
    table.setLayoutData(gridData1);

    table.setLinesVisible(true);
    table.setHeaderVisible(true);

    String[] titles = { methodHeader, messageHeader, parametersHeader, resultHeader, deltaHeader, assertionHeader};
    for (int i = 0; i < titles.length; i++) {
        TableColumn column = new TableColumn(table, SWT.LEFT, i);
        column.setText(titles[i]);
        table.getColumn(i).pack();
    }


    addDataToTableModel(methodList);

    GridData gridData2 = new GridData(SWT.END, SWT.END, false, false);
    gridData2.widthHint = 80;

    buttonOk = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonOk.setText(ok);
    buttonOk.setLayoutData(gridData2);

    buttonCancel = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonCancel.setText(cancel);
    /*GridData gridData3 = new GridData(SWT.END, SWT.END, false, false);
    gridData3.widthHint = 80;*/
    buttonCancel.setLayoutData(gridData2);

    buttonHelp = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonHelp.setText(help);
    /*GridData gridData4 = new GridData(SWT.END, SWT.END, false, false);
    gridData4.widthHint = 80;*/
    buttonHelp.setLayoutData(gridData2); 

This is how my frame looks when I run this code左2个按钮,右1个按钮。

EDIT1: 尝试对每个按钮使用不同的GridData:

    GridData gridData2 = new GridData(SWT.END, SWT.END, false, false);
    gridData2.widthHint = 80;

    buttonOk = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonOk.setText(ok);
    buttonOk.setLayoutData(gridData2);

    buttonCancel = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonCancel.setText(cancel);
    GridData gridData3 = new GridData(SWT.END, SWT.END, false, false);
    gridData3.widthHint = 80;
    buttonCancel.setLayoutData(gridData3);

    buttonHelp = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonHelp.setText(help);
    GridData gridData4 = new GridData(SWT.END, SWT.END, false, false);
    gridData4.widthHint = 80;
    buttonHelp.setLayoutData(gridData4); 

按钮的结果相同。

1 个答案:

答案 0 :(得分:3)

使用4列作为布局:

final GridLayout layout1 = new GridLayout(4, false);

,并使用空白填充标签抓住按钮左侧的空间:

Label filler = new Label(composite, SWT.LEAD);
filler.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

GridData gridData2 = new GridData(SWT.END, SWT.END, false, false);
gridData2.widthHint = 80;

Button buttonOk = new Button(composite, SWT.PUSH | SWT.CENTER);
buttonOk.setText("OK");
buttonOk.setLayoutData(gridData2);

gridData2 = new GridData(SWT.END, SWT.END, false, false);
gridData2.widthHint = 80;

Button buttonCancel = new Button(composite, SWT.PUSH | SWT.CENTER);
buttonCancel.setText("Cancel");
buttonCancel.setLayoutData(gridData2);

gridData2 = new GridData(SWT.END, SWT.END, false, false);
gridData2.widthHint = 80;

Button buttonHelp = new Button(composite, SWT.PUSH | SWT.CENTER);
buttonHelp.setText("Help");
buttonHelp.setLayoutData(gridData2);