如何将参数传递给NetBeans上的initCompontents方法[Java]

时间:2016-03-16 14:16:52

标签: java netbeans

如上所述,我想在NetBeans上将参数(DefaulStyledDocument doc)传递给initComponents方法,但它是一种唯一读取的方法。 该参数应由JtextPane使用,并在其中初始化。 如何在不创建新方法myIntiComponents(DefaultStyledDocument doc)的情况下传递该参数? 这是我到目前为止写的:

    package AppPackage;

    import java.awt.Color;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DefaultStyledDocument;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyleContext;

    /**
     *
     * 
     */
    public class MainFrame extends javax.swing.JFrame {

        /** Creates new form MainFrame */
        public MainFrame() {


            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 attrPink = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.PINK);
            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());
                    int before = findLastNonWordChar(text, offset);
                    if (before < 0) before = 0;
                    int after = findFirstNonWordChar(text, offset + str.length());
                    int wordL = before;
                    int wordR = before;

                    while (wordR <= after) {
                        if (wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) {                     

                            if (text.substring(wordL, wordR).matches("(\\W)*(System)"))
                                setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                            else if (text.substring(wordL, wordR).matches("(\\W)*(cpu)"))
                                setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                            else if (text.substring(wordL, wordR).matches("(\\W)*(motherboard)"))
                                setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                            else if (text.substring(wordL, wordR).matches("(\\W)*(gpu)"))
                                setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                            else if (text.substring(wordL, wordR).matches("(\\W)*(screen)"))
                                setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                            else if (text.substring(wordL, wordR).matches("(\\W)*(ram)"))
                                setCharacterAttributes(wordL, wordR - wordL, attrGray, false);
                            else if (text.substring(wordL, wordR).matches("(\\W)*(psu)"))
                                setCharacterAttributes(wordL, wordR - wordL, attrRed, false); 
                            else if (text.substring(wordL, wordR).matches("(\\W)*(time)"))
                                setCharacterAttributes(wordL, wordR - wordL, attrPink, false);

                            else
                                setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
                            wordL = wordR;
                        }
                        wordR++;
                    }
                }


            myInitComponents(doc);         
        }

        private void myInitComponents(DefaultStyledDocument doc) {

         //all initilizations made by netbeans for my GUI..
         textField = new javax.swing.JTextPane(doc);
         //other initializations..
         }
}

所以,我到目前为止所做的是将方法initComponets复制到另一个名为myInitComponents(DefaultStyledDocument doc)的方法,在构造函数中删除对initComponents方法的调用。

0 个答案:

没有答案