0

2 文字以内の単語を強調表示できるようにする必要があります。例えば、 :

//Highlight whatever is in between the two quotation marks
String keyChars = " \" \" ";

私はこれについて何週間も苦労してきました。私はそれを調べ、ソースコードを読み、コードを書きましたが、どうすればこれができるかまだわかりません。

4

1 に答える 1

1

次のコード スニペットは機能します。

ed=new JEditorPane();
ed.setText("This \"text\" contains \"quotes\". The \"contents\" of which are highlighted");
Pattern pl;
pl=Pattern.compile("\"");
Matcher matcher = pl.matcher(ed.getText());
int end=0,beg=0;
while(matcher.find())
{
    beg=matcher.start();
    matcher.find(); //finding the next quote
    end=matcher.start();
    DefaultHighlightPainter d =  new DefaultHighlightPainter(Color.YELLOW);
    try {
        ed.getHighlighter().addHighlight(beg+1, end,d);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
}
于 2013-04-09T05:32:00.517 に答える