【问题标题】:How to change color to specific symbols in a JTextPane [java]如何将颜色更改为 JTextPane [java] 中的特定符号
【发布时间】:2016-03-19 14:49:35
【问题描述】:

我正在尝试制作一个包含一种文本编辑器的 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();
    }
}

我的问题是符号“->”的颜色何时应该改变。事实上,如果我用“->”代替“私人”或“公共”,那是行不通的。 你能帮我找到一种方法吗?谢谢

【问题讨论】:

    标签: java swing jtextpane jdk1.6


    【解决方案1】:

    查看此示例(摘自以前的帖子):

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

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 2012-03-02
      • 2014-09-28
      • 1970-01-01
      相关资源
      最近更新 更多