JTextArea的字不改变颜色[JAVA]

时间:2016-03-10 13:28:03

标签: java swing jtextarea jdk1.6

我实现了这个代码,其目的是拥有一个看起来像编辑器的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);




    }
}                                       

没有错误或消息被捕获,但这些字都没有改变前景色。 为什么这段代码什么都不做? 谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

  

为什么这段代码什么都不做?

JTextArea不支持属性。所有文字必须是单一颜色。 PlainDocument忽略这些属性。

您需要使用一个JTextPane,它使用支持属性的StyledDocument。

请参阅Text Component Features上Swing教程中的部分,了解支持不同属性的文本窗格的工作示例。