2

だから私は簡単なエディタを書こうとしています。魔女が"文字の間にあるすべての文字を灰色にしたい。それを行うフラグメントは次のとおりです。

class MainPanel extends JPanel {

private int WIDTH = 800;
private int HEIGHT = 500;
private JTextPane codePane = new JTextPane(); //Pole, w które wpisywany jest kod
private JLabel codeLabel = new JLabel("JNotepad");
private StyledDocument doc = codePane.getStyledDocument();
private final String[] keywords; //Słowa kluczowe
private final Map<String, String> shortcuts = new HashMap<>(); //syso -> System.out.println() itp.

MainPanel() {
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setLayout(new BorderLayout());
    //Dodanie głównego pola w polu przewijanym
    JScrollPane scroll = new JScrollPane(codePane);
    add(scroll, BorderLayout.CENTER);
    add(codeLabel, BorderLayout.SOUTH);
    codePane.addKeyListener(new KeyHandler());
    codePane.setFont(new Font("Monospaced", Font.PLAIN, 15));
    //Załadowanie słów kluczowych
    Scanner in = new Scanner(getClass().getResourceAsStream("res/keywords.txt"));
    List<String> words = new LinkedList<>();
    while (in.hasNext()) {
        words.add(in.nextLine());
    }
    keywords = words.toArray(new String[words.size()]);
    in.close();
}


private class KeyHandler extends KeyAdapter {

    @Override
    public void keyReleased(KeyEvent ev) {
        highlight();
    }


    private void highlight() {
        String code = codePane.getText();
        //Zmiana koloru słów kluczowych
        String[] words = code.replaceAll("\\(|\\)|\\{|\\}|\\[|\\]", " ").split("\\s");
        int lastIndex = 0;
        for (int a = 0; a < words.length; a++) {
            SimpleAttributeSet set = new SimpleAttributeSet();
            if (Arrays.asList(keywords).contains(words[a])) {
                StyleConstants.setForeground(set, Color.BLUE);
            }
            doc.setCharacterAttributes(lastIndex, lastIndex + words[a].length(), set, true);
            //Zwiekszenie ostatniego indexu
            lastIndex += words[a].length() + 1; //+1 bo jeszcze spacja
        }
    }
}

}

「」が発生すると、文字は灰色になりますが、最初の「」記号の後にすべての文字が表示されます。このコードの何が問題になっていますか?編集:ここにあります、これは完全なコードです。

4

2 に答える 2

3

setCharacterAttributes()メソッドの2番目の引数は長さであり、終了インデックスではありません。それはあなたの問題でした。

        boolean isString = false;
        char[] text = code.toCharArray();
        for (int i = 0; i < text.length; i++) {
            if (text[i] == '\"') {
                isString = !isString;
                if(!isString) {
                    document.setCharacterAttributes(i, 1, attributes, true);
                }
            }
            if (isString) {
                document.setCharacterAttributes(i, 1, attributes, true);
            }
        }

元の質問は数行のコードで短くなりましたが、mKorbelには正しいものがあります。

... in this case (it's a) job for DocumentFilter, never to use KeyListener for JTextComponent.

You should check that, it could help: How to Write a Document Listener

于 2013-03-02T18:41:38.200 に答える
0

条件が真だからです。if (string)本当になります。String一度trueが与えられるのでequals("\""))、次の文字列に続きます。

if (string){.....}ブロックで、最後に作るstring=false

于 2013-03-02T18:33:03.787 に答える