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);
}
}
});
}
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);
}
}
});
}
t.setForeground(new Color(Color.Red));
[email protected]
或者[email protected]
这种方式只能对整个文本内容染色,现在要求的是对其中关键字染色。请问如何进行
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);
}
}
});
}
.......
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);
}
}
});
}
匹配关键子多处出现问题的。下面我帮你写了一个:
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);
}
}
});
}
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);
}
}