【问题标题】:Composite with label aligned left and buttons right复合标签左对齐,按钮右对齐
【发布时间】:2018-02-24 00:09:32
【问题描述】:

我在一个 Eclipse 插件上工作,并且在某个时刻显示了一个弹出窗口。在弹出的对话框中,我想创建一个区域,其中左侧有一个标签,右侧有两个按钮。

public void createBottom(Composite parent) {
    Composite composite = new Composite(parent, SWT.FILL | SWT.WRAP | SWT.BORDER);
    GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
    composite.setLayoutData(gridData);
    composite.setLayout(new GridLayout(3, false));

    addLabel(composite);
    addButton1(composite);
    addButton2(composite);
}

目前结果如下:

虽然我期待更像这样的东西:

我怎样才能将左侧的Label 和右侧的两个按钮对齐?

【问题讨论】:

  • 什么是addLabeladdButton1addButton2
  • @greg-449 他们不是在暗示吗? 3 种方法添加了 3 项。 addLabel 添加 TestaddButton1 添加 Ok 按钮,addButton2 添加 Close 按钮。
  • “建议”不好。你需要向我们展示一个正确的minimal reproducible example 忽略代码块只会让响应变得更加困难。
  • @greg-449 很抱歉,我会记住的

标签: eclipse eclipse-plugin swt


【解决方案1】:

第一个SWT.FILLSWT.WRAPComposite不是有效样式。控件的 Javadoc 指定了您可以使用的样式。

使用类似的东西:

Composite composite = new Composite(parent, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
composite.setLayoutData(gridData);
composite.setLayout(new GridLayout(3, false));

Label label = new Label(composite, SWT.BEGINNING);
label.setText("Test");
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

Button but1 = new Button(composite, SWT.PUSH);
but1.setText("OK");
but1.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));

Button but2 = new Button(composite, SWT.PUSH);
but2.setText("Close");
but2.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));

标签的布局数据占据了复合材料中的额外空间,并且按钮具有末端对齐方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2012-01-15
    • 2021-08-25
    • 2017-01-10
    • 2021-05-10
    • 2012-10-13
    相关资源
    最近更新 更多