2013-03-09 2 views
0

가이 코드JTextPane의 새로운 라인

StringBuilder sb = new StringBuilder(); 
sb.append("<span style=\"color:black\">--------------</span> <br>"); 
sb.append("<span style=\"color:red\">Error." + e.toString() + "</span> <br>"); 
sshoutput.setContentType("text/html"); 
sshoutput.setText(sb.toString()); 

을 사용하여 새 라인을 만들 수 있도록하지만 난 다른 텍스트와 함께이 한 번 더 할 때에만이

오류 후 두 번째 텍스트 하지를 보여줍니다 . "+ e.toString()

죄송합니다 제 영어는 잘되지 않습니다. 당신이 이해할 수 있기를 바랍니다.

+0

코드로 약간의 예외를 자세히 설명 할 수 있습니까? –

+0

왜 StringBuilder 객체는'br'이지만'sb.append'를하고 있습니까? – SudoRahul

+0

불행히도, 나는 정말로 이해하지 못합니다. 기존 텍스트에 다른 텍스트를 추가 하겠지만 대신 텍스트를 대체한다는 의미입니까? –

답변

3

지금은 JTextPane가 작업하고 내가 할 것은 해요 :

JTextPane pane = new JTextPane(); 
StyledDocument doc = pane.getStyledDocument(); 

을 그리고 내가 사용하는 장소에 문자열을 삽입 할 수 있습니다

doc.insertString(STRING POSITION, STRING, null); 

나는이 방법으로 예외가 없습니다. 사용하여 스타일 문자에 쉬운 방법이있다 :

SimpleAttributeSet set = new SimpleAttributeSet(); 
//Here you modify set. Set is collection of 
//various style instructions 
//(letters color, bolded, italic, background color etc.) 
//You modify set using StyleConstants class. 
doc.setCharacterAttributes(START, LENGTH, set, true); 

편집 : 텍스트 창을 생성하고이 '안녕하세요'를 스타일의 글을 예 :

JTextPane pane = new JTextPane(); 
StyledDocument doc = pane.getStyledDocument(); 
doc.insertString(0, "Hello", null); 
SimpleAttributeSet set = new SimpleAttributeSet(); 
StyleConstans.setForeground(set, Color.RED); 
doc.setCharacterAttributes(0, 5, set, true); 
doc.insertString(5, "World!", null); 
SimpleAttributeSet set = new SimpleAttributeSet(); 
StyleConstans.setForeground(set, Color.BLUE); 
doc.setCharacterAttributes(5, 6, set, true); 

가 JPanel의에 추가 GridLayout (1, 1)을 사용하면 빨간색 문자열 "Hello"와 파란색 문자열 "World"가있는 텍스트 창이 나타납니다.

+0

간단한 예제를 보여줄 수 있습니까? –