private void setTextColor(final JTextArea sourcePane,
final int offset, final int endPos, final Color color) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, color);
if (offset != endPos) {
StyledDocument doc = (StyledDocument) sourcePane.getDocument();
doc.setCharacterAttributes(offset, endPos - offset, attr, false);
}
}
});
}

解决方案 »

  1.   

    JTextArea t = new JTextArea();
    t.setForeground(new Color(Color.Red));
      

  2.   

    很简单,如果你要例子我可以给你一个,因为eclipse自带了一个例子,我想用swing也差不多可以实现,我以前写过一个java编辑器 :)
      

  3.   

    完全可以模仿editplus写,采取关键字匹配模式
      

  4.   

    谢了,好诶,好诶发个例子给我。
    [email protected]
    或者[email protected]
      

  5.   

    to:HawaiiLeo(罗马数字) 
       这种方式只能对整个文本内容染色,现在要求的是对其中关键字染色。请问如何进行
      

  6.   

    改为JTextPane就可以了,我运行过,没问题的
    private void setTextColor(final JTextPane sourcePane,
                                  final int offset, final int endPos, final Color color) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    javax.swing.text.MutableAttributeSet attr = new javax.swing.text.SimpleAttributeSet();
                    javax.swing.text.StyleConstants.setForeground(attr, color);
                    if (offset != endPos) {
                        javax.swing.text.StyledDocument doc = (javax.swing.text.StyledDocument) sourcePane.getDocument();
                        doc.setCharacterAttributes(offset, endPos - offset, attr, false);
                    }
                }
            });
        }
      

  7.   

    www.javagarden.net里有个例子。
      

  8.   

    谢谢楼上各位,现在我需要将文本里面的关键字变色,例将“String”“Array”变色,请问该如何下手
      

  9.   

    JTextPane jtxtPane = new JTextPane();
    .......
    String text = jtxtPane();
    if (text.indexOf("String") >= 0)
      setTextColor(jtxtPane, text.indexOf("String"),  "String".length(), Color.red);
    .........
    .........
    private void setTextColor(final JTextPane sourcePane,
                                  final int startPos, final int length, final Color color) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    javax.swing.text.MutableAttributeSet attr = new javax.swing.text.SimpleAttributeSet();
                    javax.swing.text.StyleConstants.setForeground(attr, color);
                    if (length > 0) {
                        javax.swing.text.StyledDocument doc = (javax.swing.text.StyledDocument) sourcePane.getDocument();
                        doc.setCharacterAttributes(startPos, length, attr, false);
                    }
                }
            });
        }
      

  10.   

    (String text = jtxtPane();)?
      

  11.   

    呵呵,没有运行直接在这里写漏了。我前面给的例子是教你怎么设颜色,只要你自己做点处理就可以
    匹配关键子多处出现问题的。下面我帮你写了一个:
    private void test() {
            jTextPane.setText("aaaabbbbccccddddeeefffaaaddd");
            String text = jTextPane.getText();
            String str = "d";
            for (int i = 0; text != null && i <text.length(); i++) {
                if (text.indexOf(str, i) >= 0) {
                    setTextColor(jTextPane, text.indexOf(str, i), str.length(), Color.red);
                    i = text.indexOf(str, i);
                }
            }
        }    private void setTextColor(final JTextPane sourcePane,
                                  final int startPos, final int length, final Color color) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    javax.swing.text.MutableAttributeSet attr = new javax.swing.text.SimpleAttributeSet();
                    javax.swing.text.StyleConstants.setForeground(attr, color);
                    if (length > 0) {
                        javax.swing.text.StyledDocument doc = (javax.swing.text.StyledDocument) sourcePane.getDocument();
                        doc.setCharacterAttributes(startPos, length, attr, false);
                    }
                }
            });
        }
      

  12.   

    setTextColor方法改动一下,没必要用invokelater
    private void setTextColor(final JTextPane sourcePane,
                                  final int startPos, final int length, final Color color) {
            javax.swing.text.MutableAttributeSet attr = new javax.swing.text.SimpleAttributeSet();
            javax.swing.text.StyleConstants.setForeground(attr, color);
            if (length > 0) {
                javax.swing.text.StyledDocument doc = (javax.swing.text.StyledDocument) sourcePane.getDocument();
                doc.setCharacterAttributes(startPos, length, attr, false);
            }
        }