Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于位置的单词着色_Java_Swing - Fatal编程技术网

Java 基于位置的单词着色

Java 基于位置的单词着色,java,swing,Java,Swing,我正在根据线条的位置为部分线条上色 使用的库是Swing。 组件是JTextPane StyledDocument doc = editorJTextPane.getStyledDocument(); Style styleRed = editorJTextPane.addStyle("Red", null); StyleConstants.setForeground(styleRed, Color.red); StyleConstants.setBold(styleRed, rootPane

我正在根据线条的位置为部分线条上色

使用的库是Swing。 组件是JTextPane

StyledDocument doc = editorJTextPane.getStyledDocument();

Style styleRed = editorJTextPane.addStyle("Red", null);
StyleConstants.setForeground(styleRed, Color.red);
StyleConstants.setBold(styleRed, rootPaneCheckingEnabled);

Style styleGreen = editorJTextPane.addStyle("Green", null);
StyleConstants.setForeground(styleGreen, Color.green);
StyleConstants.setBold(styleGreen, rootPaneCheckingEnabled);

String[] allLines = editorJTextPane.getText().split("\n");

int offSet1 = 0;
int offSet2 = 5;

for(int i=0; i<allLines.length; i++)
{
  line = allLines[i];
  lineLength = line.length() + 1;

  doc.setCharacterAttributes(offSet1, 4, editorJTextPane.getStyle("Red"), true);    

  doc.setCharacterAttributes(offSet2, 15, editorJTextPane.getStyle("Green"), true);

  offSet1 = offSet1 + lineLength;
  offSet2 = offSet2 + lineLength;
}
StyledDocument doc=editorJTextPane.getStyledDocument();
Style styleRed=editorJTextPane.addStyle(“红色”,null);
设置前景(styleRed,Color.red);
StyleConstants.setBold(styleed,rootPaneCheckingEnabled);
Style styleGreen=editorJTextPane.addStyle(“绿色”,null);
设置前景(styleGreen,Color.green);
setBold(styleGreen,rootPaneCheckingEnabled);
字符串[]allLines=editorJTextPane.getText().split(“\n”);
int offSet1=0;
int offSet2=5;

对于(int i=0;i您必须使用JDK 7,对于DefaultStyledDocument.java中的第507行

public void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace) {
         .......
         AttributeSet sCopy = s.copyAttributes();//Line 507 here
         .......
     }

因此,您传入的AttributeSet为空。您发布的代码对我有效,因此您的代码在其他位置一定出错。

您必须使用JDK 7,用于DefaultStyledDocument.java中的第507行

public void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace) {
         .......
         AttributeSet sCopy = s.copyAttributes();//Line 507 here
         .......
     }

因此,您传入的属性集为空。您发布的代码对我有效,因此您的代码在其他地方一定出错了。

谢谢,满是EEL的Hovercrft要更快获得更好的帮助,请发布一个。您在哪里定义
doc
以及在哪里定义
editorJTextPane
?从错误中可以看出问题是一个问题尚未初始化的ct…调试器显示editorJTextPane=(javax.swing.JTextPane)javax.swing.JTextPane[,0,0995x556,无效,layout=javax.swing.plaf.basic.BasicText1其中有任何线索吗?何时调用此代码?检查调用时doc和editorJTextPane是否不为空谢谢,悬停满eels为了更快地获得更好的帮助,发布一条。您在哪里定义
doc
,在哪里定义
editorJTextPane
?它显示为from错误:问题是一个尚未初始化的对象…调试器显示editorJTextPane=(javax.swing.JTextPane)javax.swing.JTextPane[,0,0995x556,无效,layout=javax.swing.plaf.basic.BasicText1有任何线索吗?何时调用此代码?在调用时检查doc和editorJTextPane是否为null