2013-04-09 3 views
0

두 문자 내에서 단어를 강조 표시 할 수 있어야합니다. 예 :Java - 두 Char 사이의 텍스트 강조 - JTextPane

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

나는 몇 주 동안이 문제에 지금까지 괴롭 히고 있습니다. 나는 그것을 찾아보고, 소스 코드를 읽고, 코드를 썼다. 그리고 아직도 내가 이것을 어떻게 할 수 있을지 알지 못했다.

+0

* "코드 작성"* 특별히 시도한 것은 무엇입니까? 어떻게 실패 했습니까? –

+0

[this] (http://stackoverflow.com/questions/13448558/highlight-a-word-in-jeditorpane/13449000#13449000) 예제에서 'JEditorPane'을 사용하지만 개념은 동일해야합니다. – MadProgrammer

+0

여기에 DefaultHighlighter.DefaultHighlightPainter 클래스를 검색하는 힌트입니다. 그리고 Regex가 무엇인지 확인하십시오. – Jayamohan

답변

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(); 
    } 
} 
+0

답변을 주셔서 감사합니다. 불행히도 제가 찾고 있었던 것이 아닙니다. 제 잘못입니다. 제 질문에 대해 충분히 명확하지 않았기 때문입니다. 내가 말해야했던 것은 두 등장 인물 사이에서 "생생한 강조"였다. 내 잘못이야. – user2228462