Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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 JTextPane设置颜色特定词_Java_Colors_Jtextpane - Fatal编程技术网

Java JTextPane设置颜色特定词

Java JTextPane设置颜色特定词,java,colors,jtextpane,Java,Colors,Jtextpane,人们认为解决方案已经确定。我想是这样,但不幸的是,结果是相同的尝试,但第三个解决方案没有产生任何变化的文字颜色。。。 我正在尝试将JTextPane中某个单词的颜色(变为红色)更改为 显示它的状态。这里有很多例子,我也尝试过一些,但最终的结果是文本保持不变或整个文本的颜色改变 我将在这里放置一段代码,因为该类相当大 .. textPane = new JTextPane(); textPane.setFont(new Font("Arial", Font.PLAIN, 12)); .. Str

人们认为解决方案已经确定。我想是这样,但不幸的是,结果是相同的尝试,但第三个解决方案没有产生任何变化的文字颜色。。。

我正在尝试将JTextPane中某个单词的颜色(变为红色)更改为 显示它的状态。这里有很多例子,我也尝试过一些,但最终的结果是文本保持不变或整个文本的颜色改变

我将在这里放置一段代码,因为该类相当大

..
textPane = new JTextPane();
textPane.setFont(new Font("Arial", Font.PLAIN, 12));
..
String productName = "PC";
String vendorName = "DELL";
String statusOfProd = "OFF";
String theObject = "Product " + productName + " Vendor " + vendorName;
String taData = theObject + "\n";
textPane.setText(taData);
if (statusOfProd.equals("OFF")){
   addColor2Pane(productName, Color.RED);
}
..
private void addColor2Pane(String value2Change, Color color2Use) {
   String theData = textPane.getText();
   int v2cIndex = theData.indexOf(value2Change);
   int v2cLen = value2Change.length();

   try {
      textPane.getHighlighter().addHighlight(v2cIndex, v2cIndex + v2cLen,
        new DefaultHighlighter.DefaultHighlightPainter(color2Use));
   }
   catch (BadLocationException e) {
       e.printStackTrace();
   }
}
..
// Attributes
protected JTextPane textPane;
public static String taData;
上述方法的结果没有影响。如果我更改“addColor2Pane” 方法将窗格中的所有文本呈现为红色,这不是我想要实现的

..
private void addColor2Pane(String value2Change, Color color2Use) {

   StyleContext sc = StyleContext.getDefaultSytleContext();
   AttributeSet aSet = sc.addAttribute(sc.getEmptySet(),   
                                       StyleConstants.Foreground, 
                                       color2Use);
   aSet = sc.addAttribute(aSet, 
                          StyleConstants.FontFamily, 
                          "Lucida Console");
   aSet = sc.addAttribute(aSet, 
                          StyleConstants.Alignment,
                          StyleConstants.ALIGN_LEFT);

   int v2cInd = theData.indexOf(value2Change);
   int v2cLen = value2Change.length();

   textPane.setCaretPosition(v2cInd);
   textPane.setCharacterAttributes(aSet, true);
   textPane.replaceSection(value2Change);

}
所需的结果是仅将productName的颜色设置为红色。建议?

可能重复“我将在此处放置一段代码,因为类相当大”,我们不需要您的全部代码或代码片段,但需要一个有效的示例来演示您的问题以及您自己解决问题的最佳方法C: