[英]Word of JTextArea don't change color [JAVA]
我實現了此代碼,其目的是擁有一個看起來像編輯器的JtextArea,其中某些關鍵字在鍵入或插入時會更改顏色。 這是我寫的代碼:
private void openBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt files", "txt", "text");
chooser.setFileFilter(filter);
int option = chooser.showOpenDialog(null);
if (option == JFileChooser.APPROVE_OPTION) {
f = chooser.getSelectedFile();
filename = f.getAbsolutePath();
String modelSpec = readSpecification();
spec = modelSpec;
final StyleContext cont = StyleContext.getDefaultStyleContext();
final javax.swing.text.AttributeSet attrGray = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.gray);
final javax.swing.text.AttributeSet attrGreen = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.GREEN);
final javax.swing.text.AttributeSet attrBlue = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);
final javax.swing.text.AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
final javax.swing.text.AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
DefaultStyledDocument doc = new DefaultStyledDocument() {
@Override
public void insertString (int offset, String str, javax.swing.text.AttributeSet a) throws BadLocationException {
super.insertString(offset, str, a);
//String text = getText(0, getLength()-1);
int before = findLastNonWordChar(spec, offset);
if (before < 0) before = 0;
int after = findFirstNonWordChar(spec, offset + str.length());
int prevWord = before;
int postWord = before;
while (postWord <= after) {
if (postWord == after || String.valueOf(spec.charAt(postWord)).matches("\\W")) {
if (spec.substring(prevWord, postWord).matches("(\\W)*(System)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(PlasmaMembrane)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(Cytosol)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(Organelle)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(Nucleous)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(volume)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrGray, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(rate)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrRed, false);
else
setCharacterAttributes(prevWord, postWord - prevWord, attrBlack, false);
prevWord = postWord;
}
postWord++;
}
}
@Override
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)*(System)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(PlasmaMembrane)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(Cytosol)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(Organelle)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(Nucleous)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(volume)"))
setCharacterAttributes(before, after - before, attrGray, false);
else if (text.substring(before, after).matches("(\\W)*(rate)"))
setCharacterAttributes(before, after - before, attrRed, false);
else {
setCharacterAttributes(before, after - before, attrBlack, false);
}
}
};
textModel.setText(spec);
}
}
沒有發現任何錯誤或異常,但是這些詞都沒有改變前景色。 為什么這段代碼什么都不做? 感謝您的幫助
為什么這段代碼什么都不做?
JTextArea不支持屬性。 所有文本必須為單一顏色。 該屬性被PlainDocument忽略。
您需要使用JTextPane,它使用支持屬性的StyledDocument。
有關支持不同屬性的文本窗格的工作示例,請參見Swing教程中有關文本組件功能的部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.