0

我有一个 JTextPane 供用户编写日记条目,他们可以通过选择文本并选择粗体或斜体等菜单项来设置样式。这些项目连接到样式编辑器工具包之一(例如 StyledEditorKit.BoldAction() )。

有没有办法检测给定文档位置的文本是否已使用这些工具包之一进行样式设置?如果是这样怎么办?

       //Create the style menu.
protected JMenu createStyleMenu() {
    JMenu menu = new JMenu("Style");

    Action action = new StyledEditorKit.BoldAction();  
    action.putValue(Action.NAME, "Bold");        
    menu.add(action);

    action = new StyledEditorKit.ItalicAction();
    action.putValue(Action.NAME, "Italic");
    menu.add(action);

    action = new StyledEditorKit.UnderlineAction();
    action.putValue(Action.NAME, "Underline");
    menu.add(action);

    menu.addSeparator();

    menu.add(new StyledEditorKit.FontSizeAction("12", 12));
    menu.add(new StyledEditorKit.FontSizeAction("14", 14));
    menu.add(new StyledEditorKit.FontSizeAction("18", 18));
    menu.add(new StyledEditorKit.FontSizeAction("36", 36));

    menu.addSeparator();

    menu.add(new StyledEditorKit.FontFamilyAction("Serif",
                                                  "Serif"));
    menu.add(new StyledEditorKit.FontFamilyAction("SansSerif",
                                                  "SansSerif"));

    menu.addSeparator();

    menu.add(new StyledEditorKit.ForegroundAction("Red",
                                                  Color.red));
    menu.add(new StyledEditorKit.ForegroundAction("Green",
                                                  Color.green));
    menu.add(new StyledEditorKit.ForegroundAction("Blue",
                                                  Color.blue));
    menu.add(new StyledEditorKit.ForegroundAction("Black",
                                                  Color.black));

    return menu;
}

任何帮助将非常感激。谢谢你。

4

1 回答 1

2

您可以查询文档中某个字符的属性:

StyledDocument doc = (StyledDocument)textPane.getDocument();
Element element = doc.getCharacterElement(position);

Boolean isItalic = element.getAttributes().getAttribute(StyleConstants.Italic);
于 2013-12-02T11:10:53.040 回答