2016-09-01 3 views
2

J'ai donc obtenu ce code de this link on SO et je l'ai eu pour faire ce que je voulais, mais après avoir reculé d'un mot d'une couleur, tous les mots précédents sont aussi cette couleur.JTextPanel mise en surbrillance de texte?

result without glitch activated

Résultat: Ces mots sont jaunes: YEL, YW. Ce mot est rouge: rd. Ces mots sont bleus: blu, be.

result with glitch activated

Résultat: Ces mots sont jaunes: YEL, YW. Ce mot est rouge: rd. Ces mots sont bleus: blu, be.

J'ai fait un retour arrière d'un des mots jaunes. Maintenant, tous mes mots sont jaunes.

code:

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

J'ai trouvé une solution:

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

Est-ce la dernière phrase colorée par votre algorithme ou est-ce le inputAttributes que sont faux? Utilisez un débogueur dans n'importe quel ide pour vérifier. – Sharcoux

Répondre

0

J'ai trouvé une solution:

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