You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JFace MessageDialog.openConfirm中实现部分文本着色?

关于SWT MessageDialog带颜色文本的实现问题

嘿,刚好碰到过类似需求!直接说结论:原生的MessageDialog.openConfirm没法实现这个效果——因为它只接受纯字符串作为消息内容,不支持富文本样式(比如给特定文本段设置不同颜色)。

不过别担心,我们可以通过自定义对话框来实现这个需求,核心是用SWT的StyledText控件来渲染带样式的文本。下面是具体的实现步骤和代码示例:

替代方案:自定义富文本确认对话框

我们可以创建一个继承自Dialog的自定义类,在里面使用StyledText控件来显示带颜色的文本,同时保留原生确认对话框的按钮布局和交互逻辑。

1. 自定义对话框类代码

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Shell;

public class StyledConfirmDialog extends Dialog {
    private String dialogTitle;
    private String messageContent;
    private boolean isConfirmed = false;

    // 构造方法,传入父Shell、标题和消息文本
    public StyledConfirmDialog(Shell parent, String title, String message) {
        super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.YES | SWT.NO);
        this.dialogTitle = title;
        this.messageContent = message;
    }

    // 打开对话框并返回用户选择结果
    public boolean open() {
        Shell dialogShell = new Shell(getParent(), getStyle());
        dialogShell.setText(dialogTitle);
        createDialogContents(dialogShell);
        dialogShell.pack();
        dialogShell.setLocationRelativeTo(getParent());
        dialogShell.open();

        // 处理事件循环
        while (!dialogShell.isDisposed()) {
            if (!getParent().getDisplay().readAndDispatch()) {
                getParent().getDisplay().sleep();
            }
        }
        return isConfirmed;
    }

    // 创建对话框内容
    private void createDialogContents(Shell shell) {
        shell.setLayout(new GridLayout(1, false));

        // 创建StyledText控件显示带样式的消息
        StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.READ_ONLY);
        styledText.setText(messageContent);
        styledText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        // 给指定文本段设置颜色样式
        StyleRange[] styleRanges = new StyleRange[3];
        // 给"a"设置红色
        int aPos = messageContent.indexOf("a");
        styleRanges[0] = new StyleRange(aPos, 1, shell.getDisplay().getSystemColor(SWT.COLOR_RED), null);
        // 给"b"设置绿色
        int bPos = messageContent.indexOf("b");
        styleRanges[1] = new StyleRange(bPos, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GREEN), null);
        // 给"c"设置蓝色
        int cPos = messageContent.indexOf("c");
        styleRanges[2] = new StyleRange(cPos, 1, shell.getDisplay().getSystemColor(SWT.COLOR_BLUE), null);
        
        styledText.setStyleRanges(styleRanges);

        // 创建确认/取消按钮区域
        Composite buttonPanel = new Composite(shell, SWT.NONE);
        buttonPanel.setLayout(new GridLayout(2, true));
        buttonPanel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));

        Button confirmBtn = new Button(buttonPanel, SWT.PUSH);
        confirmBtn.setText("确认");
        confirmBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        confirmBtn.addListener(SWT.Selection, e -> {
            isConfirmed = true;
            shell.close();
        });

        Button cancelBtn = new Button(buttonPanel, SWT.PUSH);
        cancelBtn.setText("取消");
        cancelBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        cancelBtn.addListener(SWT.Selection, e -> {
            isConfirmed = false;
            shell.close();
        });

        // 设置默认按钮为确认按钮
        shell.setDefaultButton(confirmBtn);
    }
}

2. 调用自定义对话框

使用方式和原生MessageDialog.openConfirm非常相似:

// 传入父Shell、标题和消息文本
StyledConfirmDialog colorConfirmDialog = new StyledConfirmDialog(shell, "title", "message a b c foobar");
boolean userChoice = colorConfirmDialog.open();

// 根据用户选择执行逻辑
if (userChoice) {
    System.out.println("用户点击了确认");
} else {
    System.out.println("用户点击了取消");
}

扩展说明

如果需要更复杂的样式(比如加粗、斜体、不同字体大小),可以扩展StyleRange的属性,例如:

// 给文本设置加粗+红色
StyleRange boldRedRange = new StyleRange();
boldRedRange.start = aPos;
boldRedRange.length = 1;
boldRedRange.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_RED);
boldRedRange.fontStyle = SWT.BOLD;

内容的提问来源于stack exchange,提问作者PixelMaster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:28:51