Java JTextPanel文本突出显示?
所以我从中得到了这个代码,我得到它来做我想做的事情,但是在我退格一种颜色的单词后,前面的所有单词也都是这种颜色 结果:这些单词是黄色的:yel,yw。这个词是红色的:rd。这些词是蓝色的:blu,be 结果:这些单词是黄色的:yel,yw。这个词是红色的:rd。这些词是蓝色的:blu,be 我把其中一个黄色的字退格了。现在我所有的话都是黄色的 代码:Java JTextPanel文本突出显示?,java,text,jtextpane,Java,Text,Jtextpane,所以我从中得到了这个代码,我得到它来做我想做的事情,但是在我退格一种颜色的单词后,前面的所有单词也都是这种颜色 结果:这些单词是黄色的:yel,yw。这个词是红色的:rd。这些词是蓝色的:blu,be 结果:这些单词是黄色的:yel,yw。这个词是红色的:rd。这些词是蓝色的:blu,be 我把其中一个黄色的字退格了。现在我所有的话都是黄色的 代码: 封装测试; 导入java.awt.Color; 导入java.awt.Font; 导入javax.swing.JFrame; 导入javax.
封装测试;
导入java.awt.Color;
导入java.awt.Font;
导入javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.JTextPane;
导入javax.swing.text.AttributeSet;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.DefaultStyledDocument;
导入javax.swing.text.StyleConstants;
导入javax.swing.text.StyleContext;
公共类ChangeFontColor扩展JFrame{
私有静态最终长serialVersionUID=212373395232340746L;
公共ChangeFontColor(){
超级(“更改字体颜色”);
setDefaultCloseOperation(关闭时退出);
设置大小(1100700);
setLocationRelativeTo(空);
final StyleContext cont=StyleContext.getDefaultStyleContext();
最终属性set attrRed=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.RED);
最终属性set attryle=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.YELLOW);
最终属性set attrBlu=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.BLUE);
DefaultStyledDocument doc=新的DefaultStyledDocument(){
私有静态最终长serialVersionUID=-344228087856301601288L;
public void insertString(int offset、String str、AttributeSet a)引发BadLocationException{
super.insertString(offset,str,a);
String text=getText(0,getLength());
int before=findLastNonWordChar(文本,偏移量);
如果(在<0之前)
前=0;
int after=findFirstNonWordChar(text,offset+str.length());
int-wordL=before;
int-wordR=before;
while(wordR=0){
如果(String.valueOf(text.charAt(index)).matches(“\\W”)){
打破
}
}
收益指数;
}
私有int findFirstNonWordChar(字符串文本,int索引){
while(索引
我找到了一个解决方案:
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();
}
}
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();
}
}
封装测试;
导入java.awt.Color;
导入java.awt.Font;
导入javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.JTextPane;
导入javax.swing.text.AttributeSet;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.DefaultStyledDocument;
导入javax.swing.text.StyleConstants;
导入javax.swing.text.StyleContext;
公共类ChangeFontColor扩展JFrame{
私有静态最终长serialVersionUID=212373395232340746L;
公共ChangeFontColor(){
超级(“更改字体颜色”);
setDefaultCloseOperation(关闭时退出);
设置大小(1100700);
setLocationRelativeTo(空);
final StyleContext cont=StyleContext.getDefaultStyleContext();
最终属性set attrRed=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.RED);
最终属性set attryle=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.YELLOW);
最终属性set attrBlu=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.BLUE);
最终属性set attrWhite=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.WHITE);
DefaultStyledDocument doc=新的DefaultStyledDocument(){
私有静态最终长serialVersionUID=-344228087856301601288L;
public void insertString(int offset、String str、AttributeSet a)引发BadLocationException{
super.insertString(offset,str,a);
String text=getText(0,getLength());
int before=findLastNonWordChar(文本,偏移量);
如果(在<0之前)
前=0;
int after=findFirstNonWordChar(text,offset+str.length());
int-wordL=before;
int-wordR=before;
while(wordR=0){
如果(String.valueOf(text.charAt(index)).matches(“\\W”)){
打破
}
}
收益指数;
}
私有int findFirstNonWordChar(字符串文本,int索引){
while(索引
我找到了一个解决方案:
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();
}
}
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();
}
}
封装测试;
导入java.awt.Color;
导入java.awt.Font;
导入javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.JTextPane;
导入javax.swing.text.AttributeSet;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.DefaultStyledDocument;
导入javax.swing.text.StyleConstants;
导入javax.swing.text.StyleContext;
公共类ChangeFontColor扩展JFrame{
私有静态最终长serialVersionUID=212373395232340746L;
公共ChangeFontColor(){
超级(“更改字体颜色”);
setDefaultCloseOperation(关闭时退出);
设置大小(1100700);
setLocationRelativeTo(空);
final StyleContext cont=StyleContext.getDefaultStyleContext();
最终属性set attrRed=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.RED);
最终属性set attryle=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.YELLOW);
最终属性set attrBlu=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.BLUE);
最终属性set attrWhite=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.WHITE);
DefaultStyledDocument文档