【问题标题】:JTextPanel text highlighting?JTextPane 文本突出显示?
【发布时间】:2016-09-01 00:22:07
【问题描述】:

所以我从this link on SO 得到了这个代码,我让它做我想做的事,但是在我退格一个颜色的单词之后,所有前面的单词也是那个颜色。

result without glitch activated

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

result with glitch activated

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

我退格了其中一个黄色单词。现在我所有的文字都是黄色的。

代码:

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

【问题讨论】:

  • 最后一句是由你的算法着色还是 inputAttributes 错误?在任何 ide 中使用调试器进行检查。

标签: java text jtextpane


【解决方案1】:

我找到了解决办法:

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

【讨论】:

    猜你喜欢
    • 2011-08-06
    • 2013-11-14
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2016-02-05
    • 2012-05-15
    • 2013-03-31
    相关资源
    最近更新 更多