Java JTextPane为所有内容着色,而不是少数字符

Java JTextPane为所有内容着色,而不是少数字符,java,swing,jtextpane,Java,Swing,Jtextpane,所以我试着写一个简单的编辑器。我想把所有介于“字符”之间的字符都涂成灰色。它的作用是: 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("J

所以我试着写一个简单的编辑器。我想把所有介于“字符”之间的字符都涂成灰色。它的作用是:

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
        }
    }
}
class主面板扩展了JPanel{
私有整数宽度=800;
私人室内高度=500;
private JTextPane codePane=new JTextPane();//Pole,w które wpisywany jest kod
专用JLabel代码标签=新JLabel(“JNotepad”);
私有StyledDocument doc=codePane.getStyledDocument();
私有最终字符串[]关键字;//Słowa kluczowe
私有最终映射快捷方式=新建HashMap();//syso->System.out.println()itp。
主面板(){
setPreferredSize(新尺寸(宽度、高度));
setLayout(新的BorderLayout());
//Dodanie głównego pola w polu przewijanym
JScrollPane scroll=新的JScrollPane(代码窗格);
添加(滚动、边框布局、居中);
添加(代码标签,BorderLayout.SOUTH);
addKeyListener(新的KeyHandler());
setFont(新字体(“等距”,Font.PLAIN,15));
//扎阿多瓦尼·索沃·克鲁佐维奇
Scanner in=new Scanner(getClass().getResourceAsStream(“res/keywords.txt”);
列表单词=新建链接列表();
while(在.hasNext()中){
words.add(在.nextLine()中);
}
关键词=words.toArray(新字符串[words.size()]);
in.close();
}
私有类KeyHandler扩展了KeyAdapter{
@凌驾
已发布公共无效密钥(KeyEvent ev){
突出显示();
}
专用空白突出显示(){
String code=codePane.getText();
//Zmiana koloru słów kluczowych
String[]words=code.replaceAll(“\\\(\\\)\\\\{\\\\\\\\\\\\\\\\\\\”,“).split(\\s”);
int lastIndex=0;
for(int a=0;a
}


当“occurs”出现时,它会将字符涂成灰色,但它会在第一个”符号后将所有字符都涂成灰色。此代码有什么问题?编辑:这是完整的代码。

因为条件为真。
如果(字符串)
将为真。
字符串
一旦
等于(\”)将为真)
。接下来的字符串将继续


if(string){….}
块中,setCharacterAttributes()方法中的第二个参数是长度,而不是结束索引。这是你的问题

        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);
            }
        }
布尔isString=false;
char[]text=code.toCharArray();
for(int i=0;i
最初的问题较短,只有几行代码,但mKorbel有权:

…在这种情况下(这是DocumentFilter的工作),永远不要将KeyListener用于JTextComponent


你应该检查一下,它可能会有帮助:

它不是。假设我写进JPane:|一些文本“string”更多文本。|“一些文本”将被涂成黑色(默认值)。然后“出现”使字符串为真。从s到g(“字符串”)我们用灰色表示。然后“再次出现,所以字符串为FALSE。这意味着“更多文本”应该是黑色的,而它是灰色的。您的解析不起作用+排除“\”是什么意思?是的。对于code=“test1\”test2\”test3 test4\“test5\”“它打印test2test5时不带引号。但我的问题不在于打印正确,而在于如何给窗格的字母着色。”。引号后的字母仍然是彩色的,这是错误的行为。我还想把定量标记也涂上颜色。如果你想让我给你完整的代码来看看这个问题,我认为是+1,但答案可能是关于DocumentListener的,在本例中是DocumentFilter的工作,永远不要为JTextComponent使用KeyListener。关于使用
DocumentFilter
来执行文本突出显示的示例,请查看