SWT按钮和信息栏对齐

时间:2015-01-08 10:34:22

标签: java layout swt jface

我正在使用SWT,使用以下代码我获得了UI,如图(1)所示。在我的应用程序信息栏中只出现某些情况,我试图使所有三个按钮始终对齐 - 侧。

GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
Composite comp = new Composite(parent, SWT.NONE);
comp.setLayout(new GridLayout(3, false));
comp.setLayoutData(gridData);
infoArea = new InformationArea(comp, "Example 5");
infoArea.create();
infoArea.setRowLimit(2);
infoArea.setVisible(false);
buttonComposite = new Composite(comp, SWT.NONE);
buttonComposite.setLayout(new GridLayout(3, false));
gridData = new GridData(SWT.END, SWT.BOTTOM, true, false);
gridData.horizontalSpan = 1;
buttonComposite.setLayoutData(gridData);
if (isAddingNewNode() && stepNode.getParent() == null) {
addButton = new Button(buttonComposite, SWT.PUSH);
addButton.setText("Add"));
gridData = new GridData(SWT.END, SWT.BOTTOM, false, false);
gridData.widthHint = 80;
addButton.setLayoutData(gridData);
addButton.setEnabled(false);
}
okButton = new Button(buttonComposite, SWT.PUSH);
okButton .setText("Ok"));
gridData = new GridData(SWT.RIGHT, SWT.BOTTOM, false, false);
gridData.widthHint = 80;
okButton.setLayoutData(gridData);
okButton.setEnabled(false);
okButtonPressed = false;

cancelButton = new Button(buttonComposite, SWT.PUSH);
cancelButton.setText("Cancel")
gridData = new GridData();
gridData.widthHint = 80;
cancelButton.setLayoutData(gridData);
cancelButton.setEnabled(true);
cancelButtonPressed = false;

我尝试将行中的grabAccessHorizo​​nalSpace(gridData = new GridData(SWT.END,SWT.BOTTOM,false,false);)(第3个参数)修改为false,如下图所示,但得到的结果如图(2)所示(3)

buttonComposite.setLayout(new GridLayout(3, false));
gridData = new GridData(SWT.END, SWT.BOTTOM, false, false);
gridData.horizontalSpan = 1;

我正在尝试构建图(4)enter image description here

中显示的UI

enter image description here

1 个答案:

答案 0 :(得分:1)

这可以让您了解如何实现您想要的目标:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell();
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(4, false));

    final Label information = new Label(shell, SWT.CENTER);
    information.setBackground(display.getSystemColor(SWT.COLOR_GRAY));
    information.setText("Some information text");
    information.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    String[] buttons = {"Add", "OK", "Cancel"};

    for(String text : buttons)
    {
        Button button = new Button(shell, SWT.PUSH);
        button.setText(text);
        button.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
    }

    Button hide = new Button(shell, SWT.PUSH);
    hide.setText("Hide/Show");
    hide.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 4, 1));
    hide.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            information.setVisible(!information.getVisible());
        }
    });

    shell.pack();
    shell.setSize(500, shell.getSize().y);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

看起来很喜欢这个:

enter image description here

enter image description here