簡體   English   中英

如何將顏色更改為JTextPane [java]中的特定符號

[英]How to change color to specific symbols in a JTextPane [java]

我正在嘗試制作一個JTextPane,其中包含一種文本編輯器,其中特定的單詞或符號具有不同的前景色或字體。 到目前為止,我已經找到了將顏色更改為單詞的方法,如下所示:

import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;

public class Test extends JFrame {

    private int findLastNonWordChar(String text, int index) {
        while (--index >= 0) {
            if (String.valueOf(text.charAt(index)).matches("\\W")) {
                break;
            }
        }
        return index;
    }

    private int findFirstNonWordChar(String text, int index) {
        while (index < text.length()) {
            if (String.valueOf(text.charAt(index)).matches("\\W")) {
                break;
            }
            index++;
        }
        return index;
    }

    public Test() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 400);
        setLocationRelativeTo(null);

        final StyleContext cont = StyleContext.getDefaultStyleContext();
        final AttributeSet attr = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
        final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
        DefaultStyledDocument doc = new DefaultStyledDocument() {
            private static final long serialVersionUID = 1L;

            @Override
            public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
                super.insertString(offset, str, a);

                String text = getText(0, getLength());
                int before = findLastNonWordChar(text, offset);
                if (before < 0) {
                    before = 0;
                }
                int after = findFirstNonWordChar(text, offset + str.length());
                int wordL = before;
                int wordR = before;

                while (wordR <= after) {
                    if (wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) {
                        if (text.substring(wordL, wordR).matches("(\\W)*(private|public|protected)")) {
                            setCharacterAttributes(wordL, wordR - wordL, attr, false);
                        } else {
                            setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
                        }
                        wordL = wordR;
                    }
                    wordR++;
                }
            }

        };
    }

    public static void main(String args[]) {
        new Test();
    }
}

我的問題是當符號' - >'的顏色應該改變時。 事實上,如果我把' - >'而不是'私人'或'公共'不起作用。 你能幫我找到辦法嗎? 謝謝

檢查以下示例(從以前的帖子中挑選):

public static void main(String[] args) {
    JTextPane jtp = new JTextPane();
    StyledDocument doc = jtp.getStyledDocument();

    Style style = jtp.addStyle("Red coloured text", null);
    StyleConstants.setForeground(style, Color.red);

    try { doc.insertString(doc.getLength(), "Style text",style); }
    catch (BadLocationException e){}

    StyleConstants.setForeground(style, Color.blue);

    try { doc.insertString(doc.getLength(), "Style text 2",style); }
    catch (BadLocationException e){}

    JFrame frame = new JFrame("ColourCheck");
    frame.getContentPane().add(jtp);
    frame.pack();
    frame.setVisible(true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM