JTextPanel文字突出显示?

时间:2016-09-01 00:22:08

标签: java text jtextpane

所以我从this link on SO得到了这个代码,我得到了我想要的东西,但在我退回一种颜色的单词之后,所有前面的单词也都是那种颜色。

result without glitch activated

结果:这些字是黄色的:yel,yw。这个词是红色的:rd。这些词是蓝色的:蓝色,是。

result with glitch activated

结果:这些字是黄色的:yel,yw。这个词是红色的:rd。这些词是蓝色的:蓝色,是。

我退缩了一个黄色的单词。现在我所有的话都是黄色的。

代码:

package test;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class ChangeFontColor extends JFrame{
private static final long serialVersionUID = 2121337395232340746L;
public ChangeFontColor() {
    super("Change Font Color");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(1100, 700);
    setLocationRelativeTo(null);

    final StyleContext cont = StyleContext.getDefaultStyleContext();
    final AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
    final AttributeSet attrYel = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.YELLOW);
    final AttributeSet attrBlu = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);

    DefaultStyledDocument doc = new DefaultStyledDocument() {
        private static final long serialVersionUID = -3442280878563016288L;

        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)*(yellow|yel|yw)")){
                        setCharacterAttributes(wordL, wordR - wordL, attrYel, true);

                    }else if((text.substring(wordL, wordR).matches("(\\W)*(red|rd)"))){
                        setCharacterAttributes(wordL, wordR - wordL, attrRed, true);

                    }else if((text.substring(wordL, wordR).matches("(\\W)*(blue|blu|be)"))){
                        setCharacterAttributes(wordL, wordR - wordL, attrBlu, true);
                    }
                    wordL = wordR;
                }
                wordR++;
            }
        }

        public void remove (int offs, int len) throws BadLocationException {
            super.remove(offs, len);

            String text = getText(0, getLength());
            int before = findLastNonWordChar(text, offs);
            if (before < 0) 
                before = 0;
            int after = findFirstNonWordChar(text, offs);

            if(text.substring(before, after).matches("(\\W)*(yellow|yel|yw)")) {
                setCharacterAttributes(before, after - before, attrYel, true);

            }else if((text.substring(before, after).matches("(\\W)*(red|rd)"))){
                setCharacterAttributes(before, after - before, attrRed, true);
            }else if((text.substring(before, after).matches("(\\W)*(blue|blu|be)"))){
                setCharacterAttributes(before, after - before, attrBlu, true);
            }
        }
    };
    JTextPane txt = new JTextPane(doc);
    txt.setBackground(Color.DARK_GRAY);
    txt.setForeground(Color.WHITE);
    txt.setCaretColor(Color.WHITE);
    txt.setFont(new Font("Serif", Font.PLAIN, 18));
    add(new JScrollPane(txt));
    setVisible(true);
}

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 static void main (String args[]) {
    new ChangeFontColor();
}
}

我找到了解决方案:

package test;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class ChangeFontColor extends JFrame{
private static final long serialVersionUID = 2121337395232340746L;
public ChangeFontColor() {
    super("Change Font Color");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(1100, 700);
    setLocationRelativeTo(null);

    final StyleContext cont = StyleContext.getDefaultStyleContext();
    final AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
    final AttributeSet attrYel = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.YELLOW);
    final AttributeSet attrBlu = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);
    final AttributeSet attrWhite = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.WHITE);

    DefaultStyledDocument doc = new DefaultStyledDocument() {
        private static final long serialVersionUID = -3442280878563016288L;

        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)*(yellow|yel|yw)")){
                        setCharacterAttributes(wordL, wordR - wordL, attrYel, true);

                    }else if((text.substring(wordL, wordR).matches("(\\W)*(red|rd)"))){
                        setCharacterAttributes(wordL, wordR - wordL, attrRed, true);

                    }else if((text.substring(wordL, wordR).matches("(\\W)*(blue|blu|be)"))){
                        setCharacterAttributes(wordL, wordR - wordL, attrBlu, true);
                    }else{
                        setCharacterAttributes(wordL, wordR - wordL, attrWhite, true);
                    }
                    wordL = wordR;
                }
                wordR++;
            }
        }

        public void remove (int offs, int len) throws BadLocationException {
            super.remove(offs, len);

            String text = getText(0, getLength());
            int before = findLastNonWordChar(text, offs);
            if (before < 0) 
                before = 0;
            int after = findFirstNonWordChar(text, offs);

            if(text.substring(before, after).matches("(\\W)*(yellow|yel|yw)")) {
                setCharacterAttributes(before, after - before, attrWhite, true);

            }else if((text.substring(before, after).matches("(\\W)*(red|rd)"))){
                setCharacterAttributes(before, after - before, attrRed, true);
            }else if((text.substring(before, after).matches("(\\W)*(blue|blu|be)"))){
                setCharacterAttributes(before, after - before, attrBlu, true);
            }else{
                setCharacterAttributes(before, after - before, attrWhite, true);
            }
        }
    };
    JTextPane txt = new JTextPane(doc);
    txt.setBackground(Color.DARK_GRAY);
    txt.setForeground(Color.WHITE);
    txt.setCaretColor(Color.WHITE);
    txt.setFont(new Font("Serif", Font.PLAIN, 18));
    add(new JScrollPane(txt));
    setVisible(true);
}

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 static void main (String args[]) {
    new ChangeFontColor();
}
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

package test;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class ChangeFontColor extends JFrame{
private static final long serialVersionUID = 2121337395232340746L;
public ChangeFontColor() {
super("Change Font Color");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1100, 700);
setLocationRelativeTo(null);

final StyleContext cont = StyleContext.getDefaultStyleContext();
final AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
final AttributeSet attrYel = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.YELLOW);
final AttributeSet attrBlu = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);
final AttributeSet attrWhite = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.WHITE);

DefaultStyledDocument doc = new DefaultStyledDocument() {
    private static final long serialVersionUID = -3442280878563016288L;

    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)*(yellow|yel|yw)")){
                    setCharacterAttributes(wordL, wordR - wordL, attrYel, true);

                }else if((text.substring(wordL, wordR).matches("(\\W)*(red|rd)"))){
                    setCharacterAttributes(wordL, wordR - wordL, attrRed, true);

                }else if((text.substring(wordL, wordR).matches("(\\W)*(blue|blu|be)"))){
                    setCharacterAttributes(wordL, wordR - wordL, attrBlu, true);
                }else{
                    setCharacterAttributes(wordL, wordR - wordL, attrWhite, true);
                }
                wordL = wordR;
            }
            wordR++;
        }
    }

    public void remove (int offs, int len) throws BadLocationException {
        super.remove(offs, len);

        String text = getText(0, getLength());
        int before = findLastNonWordChar(text, offs);
        if (before < 0) 
            before = 0;
        int after = findFirstNonWordChar(text, offs);

        if(text.substring(before, after).matches("(\\W)*(yellow|yel|yw)")) {
            setCharacterAttributes(before, after - before, attrWhite, true);

        }else if((text.substring(before, after).matches("(\\W)*(red|rd)"))){
            setCharacterAttributes(before, after - before, attrRed, true);
        }else if((text.substring(before, after).matches("(\\W)*(blue|blu|be)"))){
            setCharacterAttributes(before, after - before, attrBlu, true);
        }else{
            setCharacterAttributes(before, after - before, attrWhite, true);
        }
    }
};
JTextPane txt = new JTextPane(doc);
txt.setBackground(Color.DARK_GRAY);
txt.setForeground(Color.WHITE);
txt.setCaretColor(Color.WHITE);
txt.setFont(new Font("Serif", Font.PLAIN, 18));
add(new JScrollPane(txt));
setVisible(true);
}

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 static void main (String args[]) {
new ChangeFontColor();
}
}