2012-01-27 4 views
2

채팅 기능이있는 네트워크 응용 프로그램을 만들고 있습니다. 채팅 중에 나는 메시지를 표시하기 위해 JTextPane 과 입력을 위해 하나를 가지고있다. 그런 다음 입력 텍스트 (굵게, 기울임 꼴, 글꼴 크기, 색상)에 스타일을 추가 할 수있는 버튼이 있습니다. 텍스트는 입력 창에서 올바르게 형식이 지정되지만 디스플레이 창으로 이동하면 (올바른 JButton을 누르면) 마지막 문자 형식 만 사용됩니다. 원래의 형식을 유지하면서 어떻게 예를 들어 내가 입력에 "안녕하세요 WORL D"를 작성하는 경우? 텍스트를 이동할 수 있습니다, 디스플레이 "안녕하세요 WORL d 개"가 표시텍스트 검색시 형식 유지

textPane 입력 창

입니다 어디 세트 :

입력 텍스트를 굵게 (다른 스타일 추가 같은) 만들기위한
final SimpleAttributeSet set = new SimpleAttributeSet(); 

코드 :

bold.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       StyledDocument doc = textPane.getStyledDocument(); 
       if (StyleConstants.isBold(set)) { 
        StyleConstants.setBold(set, false); 
        bold.setSelected(false); 
       } else { 
        StyleConstants.setBold(set, true); 
        bold.setSelected(true); 
       } 
       textPane.setCharacterAttributes(set, true); 
      } 
     }); 
을 디스플레이 창에 입력 창에서 텍스트를 이동

코드 : // 유래 :

getInput.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       String input = textPane.getText(); 
       textPane.setText(""); 
       if(!input.endsWith("\n")){ 
        input+="\n"; 
       } 
       StyledDocument doc = displayPane.getStyledDocument(); 
       int offset = displayPane.getCaretPosition(); 
       try { 
        doc.insertString(offset, input, set); 
       } catch (BadLocationException ex) { 
        Logger.getLogger(ChatComponent.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
     }); 
+0

을 병합 예제를 사용하여 당신이 문서는 HTTP의 지속을 반환 않았다하시기 바랍니다 것. com/questions/9022366/올바른 스타일의 텍스트 검색 – mKorbel

+0

예 문제를 개선하고 내가 수행 한 작업이 명확 해졌습니다. – Giannis

+0

잠재적 인 응답 – mKorbel

답변