【问题标题】:Can I make eclipse SWT UI to look like Eclipse plugin UI?我可以让 Eclipse SWT UI 看起来像 Eclipse 插件 UI 吗?
【发布时间】:2016-06-28 15:24:35
【问题描述】:

我正在向 Eclipse 添加一些功能,就像在 Eclipse 插件中一样

我有一个项目是复选框选择(见图)

    final ListSelectionDialog dialog = new ListSelectionDialog(window.getShell(), list,
            ArrayContentProvider.getInstance(), new LabelProvider(), "");
    dialog.setTitle("Create Events");
    dialog.setHelpAvailable(false);
    dialog.setMessage("Potential events in code:");
    dialog.create();
    dialog.getOkButton().setText("Create");
    dialog.open();

还有一个包含单选按钮的项目(我找不到像 ListSelectionDialog 这样的单选按钮

public void display(List<String> list){
    Display.getDefault().asyncExec(new Runnable(){
        public void run() {
            Display display = Display.getDefault();
            Shell shell = new Shell(display);
            shell.setLayout(new GridLayout());
            shell.setText("Create Object");
            Label label = new Label(shell, SWT.NULL);
            label.setText("Entry point: ");
            for (String item: list){
                Button btn  = new Button(shell, SWT.RADIO);
                btn.setText(item);                    
            }
            Composite composite = new Composite(shell, SWT.NULL);
            composite.setLayout(new RowLayout());

            Button createButton = new Button(composite, SWT.PUSH);
            createButton.setText("Create");
            Button cancelButton = new Button(composite, SWT.PUSH);
            cancelButton.setText("Cancel");
            shell.setSize(400, 200);
            shell.open ();
            while (!shell.isDisposed ()) {
                if (!display.readAndDispatch ()) display.sleep ();
            }
            shell.dispose ();
                }
        });
}

正如您所见,基于 SWT 的项目看起来格格不入,并且与 Eclipse 的 UI 不一致。 有没有可能让它看起来像另一个?

【问题讨论】:

  • 所有 Eclipse 对话框都使用 SWT,通常也使用 JFace Dialog,它提供了更高级别的接口,但基本上仍然是 SWT。我不清楚你关心对话的哪些部分。
  • 第一,左上角图标不同第二,窗口的大小(我强制它为400,200)但并不理想(就像另一个窗口的自动检测大小一样)三、创建和取消按钮看起来不一样 另外,我假设我需要添加自己的监听器?到选定的收音机和取消/创建 btns?
  • 使用JFace Dialog,你可以使用同样的getOKButton()调用来改变OK按钮文本。

标签: java eclipse eclipse-plugin swt


【解决方案1】:

使用提供标准外观的 JFace Dialog。比如:

public class CreateDialog extends Dialog
{
  public CreateDialog(final Shell parentShell)
  {
    super(parentShell);
  }

  @Override
  protected void configureShell(final Shell newShell)
  {
    super.configureShell(newShell);

    newShell.setText("Create Object");
  }

  @Override
  protected void createButtonsForButtonBar(Composite parent)
  {
    createButton(parent, IDialogConstants.OK_ID, "Create", true);
    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
 }

  @Override
  protected Control createDialogArea(Composite parent)
  {
    Composite body  = (Composite)super.createDialogArea(parent);

    String [] list = {"e", "e1"};

    Label label = new Label(body, SWT.NULL);
    label.setText("Entry point: ");

    for (String item: list) {
        Button btn = new Button(body, SWT.RADIO);
        btn.setText(item);
    }

    return body;
  }
}

【讨论】:

  • 嘿,格雷格,非常感谢您的回答,它看起来确实很棒我有一些后续行动 a) 我可以在 CreateDialog 构造函数中传递一个列表,所以我不会有硬编码列表在 createDialogArea 中? b) 为什么 shell final 在构造函数中? c) 如果我想返回选定的单选按钮,是否需要在此调用中添加另一个方法来监听 OK_ID 按钮? d) 谁会自动调用 createButtonsForButtonBar/createDialogArea/configureShell?我没有看到对他们的任何要求,所以我有点困惑它是如何自动创建窗口的
  • 是的,您可以将列表传递给构造函数。 Shell 是最终的,因为它没有更改,我已将 Eclipse 设置为尽可能添加最终结果,但忘记在此处发布之前对其进行编辑。覆盖okPressed 以处理OK 按钮按下。其他方法都是基类 Dialog 中代码的覆盖,并在对话 create/open 调用期间调用。
  • 最后一个跟进,因为我使用 for 循环 ``` for (String item: list) { Button btn = new Button(body, SWT.RADIO); ``` 因为我需要找出选择了哪个收音机,我应该将所有 btns 存储在 Button[] 数组中,当 okPressed 遍历该列表并调用 isSelected 时,还是有更好、更正确的方法来做这在 JFace 中?
  • 真的没有更好的办法了。
猜你喜欢
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 2013-06-13
  • 2014-10-17
相关资源
最近更新 更多