带有自定义按钮标题的 SWT MessageBox

2024-02-18

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

    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);

}

它工作正常,但我的按钮标题是“中止”、“重试”、“忽略” 我想要自定义按钮标题,例如“覆盖”、“重命名”、“取消”。 怎样才能做到呢?
请帮忙。

***


这是一个关于如何自己做的非常简单的例子Dialog在 SWT 中(有更舒适的方法来做到这一点JFace http://www.vogella.com/articles/EclipseDialogs/article.html#tutorialjface_owndialog尽管):

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();
    }
}

它看起来像这样:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有自定义按钮标题的 SWT MessageBox 的相关文章

随机推荐

  • 失败后自动重新部署

    我使用 VSTS 在每次提交到特定分支后自动发布我的应用程序 任务基本上是 重新创建数据库 如果第一个没问题 gt 部署 API 和 Web 如果第二个没问题 gt 部署并重新启动一些 Windows 服务 在第二步中 有时会由于某些连接或
  • Couchbase 文档 ID 生成

    我有一个与 couchbase 6 0 集成的 Springboot 应用程序 我读过 如果一个键用 Id 注释 那么它将保存为文档 ID 并且不会成为 json 的一部分 然后我在键上一起使用了 Id和 Field 但该字段仍然没有出现在
  • 如何在 Jupyter / IPython 中覆盖之前的打印行

    我完全清楚print Blah end r 但这在 Jupyter IPython 笔记本中不起作用 因为它在终端中通常不打印任何内容 或者如果操作非常快 则会触发异常 难道这就是不可能吗 谢谢 None
  • 单独读取数据 fscanf

    我正在尝试从文件中读取数字 该文件具有以下格式 2 4 5 7 3 2 4 7 我尝试使用fscanf收集直到换行符的所有数字并将数字存储在数组中 与第二行相同 但直接从第二行开始不会再次开始并将其存储在另一个数组中 是否可以使用以下方法来
  • 如何修复错误:此类与键 tableView 的键值编码不兼容。 [复制]

    这个问题在这里已经有答案了 我做了一个应用程序Table View and Segmented Control 这是我第一次 我正在使用一些代码和一些教程 但它不起作用 当我运行我的应用程序时 它崩溃了 并在日志中显示此错误 MyAppli
  • 在 shell/bash 中的两个命令之间连接输入和输出

    我有两个 UNIX 程序 A 和 B 它们从 stdin stdout 读取和写入 我的第一个问题是如何将 A 的 stdout 连接到 B 的 stdinandB 的标准输出到 A 的标准输入 即 类似 A B而是双向管道 我怀疑我可以通
  • 为什么我的库无法在 CocoaPods 网站上扩展?

    我正在尝试通过 CocoaPods 网站访问我的图书馆信息 但无法访问 所有其他库似乎都能够扩展 我的是 MKWeatherUndergroundKit 这是我的 podspec Pod Spec new do s s name MKWea
  • Ember.js 中的路由采用什么样的单元测试解决方案? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我正在寻找在 Ember js 应用程序中对路由进行单元测试的最佳解决方案 我找到了两种解决方案 我希望你告诉我什么是最适合你的 这两个实现可以
  • 正则表达式匹配字符串的可选结尾

    鉴于以下情况 John Smith John Smith 123 John Smith 123 456 我想捕捉 John Smith John Smith 123 John Smith 123 456 什么样的 Java 正则表达式可以让
  • cURL 中的主机名和路径分开

    使用 cURL 时有没有办法将主机名和路径分开 我想向服务器本身发出请求 而不是向特定资源发出请求 如下 IETF 所述 Request URI 的四个选项取决于请求的性质 要求 星号 表示该请求不适用于 特定资源 但仅限于服务器本身 并且
  • QSocketNotifier:只能与以 QThread 启动的线程一起使用

    我仅使用一个非常基本的应用程序并且没有套接字或任何奇特的东西就收到以下错误 QSocketNotifier 只能与以 QThread 启动的线程一起使用 Machine Fedora 32 工作站版 Python 3 8 PyQt5 Fed
  • v4.app.Fragment 中 getLoaderManger() 和 getActivity().getSupportLoaderManager() 之间的区别

    getLoaderManager from android support v4 app Fragment and getSupportLoaderManager from android support v4 app FragmentAc
  • 如何将 XCTAssertNil 与可选结构一起使用?

    更新 2016 年 3 月 23 日我刚刚测试了下面的原始示例代码 它在 XCode 7 3 中编译得很好 看起来 XCTAssertNil 已更新以获取类型表达式 throws gt Any 因此 可能不再需要这个问题和答案 除了旧版本编
  • Google Charts API 使用 $(document).ready 方法显示空白屏幕

    我有几个函数可以使用 Google Charts API 实例化各种图表 当我在没有 jQuery 的情况下调用它们时 document ready方法 一切正常 但用这种方法 我看到的是空白屏幕 Why function drawColu
  • 遇到:json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)

    我拿到json decoder JSONDecodeError Expecting value line 1 column 1 char 0 当我尝试访问我创建的 json 文件中的值时 我运行了下面的运行文件 似乎存在这个解码器问题 但是
  • 相机2最大1440x1080

    我正在尝试使用横向 1920x1080 显示正常的全高清预览Camera2 但相机返回 1440x1080 作为最高分辨率 使用旧相机 android hardware camera 同一设备没有这样的问题 我究竟做错了什么 CameraM
  • NetBeans 作为 ColdFusion 脚本的编辑器?

    是否有任何已知的 NetBeans 插件可用于 ColdFusion 脚本的编辑 格式化和颜色编码 恐怕没有 有些人将非常基本的语法着色和正确的注释组合在一起 但没有插件 也没有 cfml 支持 你最好的选择是基于日食CFEclipse h
  • Golang 与 docker 镜像内的 CGO 交叉编译

    要求 应用程序必须容器化为 docker 镜像 并且需要支持arm64 and amd64架构 Codebase 这是一个golang应用程序 需要使用git2go https github com libgit2 git2go图书馆并且必
  • Android 颜色叠加 - PorterDuff 模式

    我有一个黑白九块可绘制对象作为视图的背景 我想在其上应用颜色 例如半透明覆盖层 使绘图可见但在其上应用所需的颜色 我想drawable setColorFilter color mode 可能会成功 但我无法弄清楚不同模式的含义 有人能给我
  • 带有自定义按钮标题的 SWT MessageBox

    我想在我的非 RCP SWT 应用程序中添加自定义按钮标题 MessageBox messageBox new MessageBox shell SWT ICON WARNING SWT ABORT SWT RETRY SWT IGNORE