带有自定义按钮标题的 SWT MessageBox [英] SWT MessageBox with custom button titles

查看:34
本文介绍了带有自定义按钮标题的 SWT MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的非 RCP SWT 应用中添加自定义按钮标题.

I want to add the custom button titles in my non-RCP SWT app.

    MessageBox messageBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
messageBox.setText("Warning");
messageBox.setMessage("Save the changes before exiting?");
 int buttonID = messageBox.open();
 switch(buttonID) {
   case SWT.YES:
  // saves changes ...
case SWT.NO:
 // exits here ...
  break;
 case SWT.CANCEL:
// does nothing ...
 }
                                    System.out.println(buttonID);

}

它工作正常,但我的按钮标题是中止"、重试"、忽略"我想要自定义按钮标题,例如覆盖"、重命名"、取消".怎么做?
请帮忙.

It works fine but i button titles are "Abort", "Retry", "ignore" I want custom button titles, like "overwrite", "rename", "cancel". How it can be done?
Please help.

*** 编辑 ********

*** EDIT ********

我也试过

MessageDialog dialog = new MessageDialog(null, "Dangerous Activity", null,
                                                    "Are you sure you want to delete?", MessageDialog.CONFIRM,
                                                    new String[]{"Preview>", "Delete", "Cancel"}, 0)
                                    {
                                    protected void buttonPressed(int buttonId) {
                                        setReturnCode(buttonId);
                                        // close(); Call close for Delete or Cancel?
                                    }};

但是 MessageDialog 要求 app 是 RCP,因此不需要导入所需的包.帮助.

But MessageDialog requires app to be RCP, therefore not importing the package required. Help.

推荐答案

这里有一个非常简单的示例,说明如何在 SWT 中创建自己的 Dialog(使用 JFace):

Here is a very simple example on how to do your own Dialog in SWT (there are more comfortable ways to do this with JFace though):

public class CustomDialog extends Dialog
{
    private String message = "";
    private Shell shell;

    public CustomDialog(Shell parent)
    {
        // Pass the default styles here
        this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        shell = parent;
    }

    public CustomDialog(Shell parent, int style)
    {
        // Let users override the default styles
        super(parent, style);
        shell = parent;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public void open()
    {
        shell.setText(getText());
        createContents(shell);
        shell.pack();
        shell.open();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
    }

    /**
     * Creates the dialog's contents
     * 
     * @param shell
     *            the dialog window
     */
    private void createContents(final Shell shell)
    {
        shell.setLayout(new GridLayout(3, true));

        // Show the message
        Label label = new Label(shell, SWT.NONE);
        label.setText(message);
        GridData data = new GridData();
        data.horizontalSpan = 3;
        label.setLayoutData(data);

        // Display the input box
        Label image = new Label(shell, SWT.NONE);
        image.setImage(shell.getDisplay().getSystemImage(SWT.ICON_ERROR));
        data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
        data.horizontalSpan = 3;
        image.setLayoutData(data);

        Button preview = new Button(shell, SWT.PUSH);
        preview.setText("Preview");
        data = new GridData(SWT.FILL, SWT.END, true, true);
        preview.setLayoutData(data);
        preview.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent event)
            {
                System.out.println("Preview");
            }
        });

        Button delete = new Button(shell, SWT.PUSH);
        delete.setText("Delete");
        data = new GridData(SWT.FILL, SWT.END, true, true);
        delete.setLayoutData(data);
        delete.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent event)
            {
                System.out.println("Delete");
            }
        });

        Button cancel = new Button(shell, SWT.PUSH);
        cancel.setText("Cancel");
        data = new GridData(SWT.FILL, SWT.END, true, true);
        cancel.setLayoutData(data);
        cancel.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent event)
            {
                shell.close();
            }
        });

        shell.setDefaultButton(preview);
    }

    public static void main(String[] args)
    {
        CustomDialog dialog = new CustomDialog(new Shell());
        dialog.setText("Title");
        dialog.setMessage("Message");

        dialog.open();
    }
}

看起来像这样:

这篇关于带有自定义按钮标题的 SWT MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆