0

ユーザーがテキストを選択し、太字や斜体などのメニュー項目を選択してスタイルを設定できる日誌エントリを作成するための JTextPane があります。これらの項目は、StyledEditorKit.BoldAction() などのスタイル付きエディタ キットの 1 つに接続されています。

特定のドキュメント位置のテキストがこれらのキットのいずれかでスタイル設定されているかどうかを検出する方法はありますか? もしそうなら、どのように?

       //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 に答える