지금은 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"가있는 텍스트 창이 나타납니다.
코드로 약간의 예외를 자세히 설명 할 수 있습니까? –
왜 StringBuilder 객체는'br'이지만'sb.append'를하고 있습니까? – SudoRahul
불행히도, 나는 정말로 이해하지 못합니다. 기존 텍스트에 다른 텍스트를 추가 하겠지만 대신 텍스트를 대체한다는 의미입니까? –