2012-12-13 37 views
1

我有一個JTextPane(1),另一個是它的邊(2)。我已經同步了它們,如果在(2)中輸入了一行,在(1)中輸入了一行代碼,但是當我插入圖像(24像素)時,(2)自動調整行長度,但(1)不調整大小當然。當調整另一行的大小時調整JTextPane的行大小

如何製作「何時(2)調整大小,調整大小(1)」的方法? (1)中插入黑色圖像(1px,24px)時,我試過圖像插入(2),但問題是,如果在(2)中插入了許多圖像,他們去一個新的行,其中(1)只是將它們添加到一行,並(1)獲得一個水平滾動條。很抱歉,但我coundn't把它縮短了...

public class SSCCE extends JFrame { 

    private JPanel contentPane; 
    int wrapme=0; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        SSCCE frame = new SSCCE(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public SSCCE() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 338); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     JScrollPane scrollName = new JScrollPane(); 
     scrollName.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); 
     scrollName.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     scrollName.setBounds(10, 11, 99, 207); 
     contentPane.add(scrollName); 

     final JTextPane name = new JTextPane(); 

     name.setEditable(false); 
     scrollName.setViewportView(name); 

     JScrollPane scrollChat = new JScrollPane(); 
     scrollChat.setBounds(114, 11, 310, 207); 
     contentPane.add(scrollChat); 

     final JTextPane chat = new JTextPane(); 
     chat.setText("Enter something!"); 
     chat.setEditable(false); 
     scrollChat.setViewportView(chat); 
     scrollChat.getVerticalScrollBar().setModel(scrollName.getVerticalScrollBar().getModel()); 

     final JTextArea chatEnter = new JTextArea(); 
     chatEnter.setBounds(10, 229, 414, 60); 
     contentPane.add(chatEnter); 

     final StyledDocument nameDoc = name.getStyledDocument(); 
     final StyledDocument chatDoc = chat.getStyledDocument(); 
     final SimpleAttributeSet right = new SimpleAttributeSet(); 
     StyleConstants.setForeground(right, Color.GRAY); 
     StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT); 
     nameDoc.setParagraphAttributes(0, nameDoc.getLength(), right, false); 

     final String TEXT_SUBMIT = "text-submit"; 
     KeyStroke enter = KeyStroke.getKeyStroke("ENTER"); 
     InputMap input = chatEnter.getInputMap(); 
     ActionMap actions = chatEnter.getActionMap(); 
     input.put(enter, TEXT_SUBMIT); 
     actions.put(TEXT_SUBMIT, new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       try { 
        String s = chatEnter.getText(); 
        s=s.replaceAll(":\\)", ":\\) "); 
        s=s.replaceAll(" ", " "); 
        //new line in name 
        String text = chatDoc.getText(0, chatDoc.getLength()); 
        int count = 1; 
        int i = text.indexOf("\n"); 
        while(i>=0){ 
         count++; 
         i=text.indexOf("\n", i + 2); 
        } 
        int totalCharacters = chat.getText().length(); 
        int lineCount = (totalCharacters == 0) ? 1 : 0; 

        try { 
         int offset = totalCharacters; // arbitrary non-zero number 
         while (offset > 0) { 
         offset = Utilities.getRowStart(chat, offset) - 1; 
         lineCount++; 
         } 
        } catch (BadLocationException ex) { 
         ex.printStackTrace(); 
        } 
        lineCount-=wrapme; 
        while(count!=lineCount) { 
         nameDoc.insertString(nameDoc.getLength(), "\n", right); 
         count++; 
         wrapme++; 
        } 
        //new line in name End 
        nameDoc.insertString(nameDoc.getLength(), "Martin\n", right); 
        chatDoc.insertString(chatDoc.getLength(), s + "\n", null); 
        chat.select(chatDoc.getLength(), chatDoc.getLength()); 
        name.select(nameDoc.getLength(), nameDoc.getLength()); 
       } catch (BadLocationException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 
      } 
     }); 

     ((AbstractDocument) chat.getDocument()).addDocumentListener(new DocumentListener() { 
      @Override 
      public void insertUpdate(final DocumentEvent de) { 
       SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
         try { 
          StyledDocument doc = (StyledDocument) de.getDocument(); 
          int start = Utilities.getRowStart(chat, Math.max(0, de.getOffset() - 1)); 
          int end = Utilities.getWordStart(chat, de.getOffset() + de.getLength()); 

          String text = doc.getText(start, (end - start)+1); 

           int i = text.indexOf(":)"); 
           while (i >= 0) { 
            final SimpleAttributeSet attrs = new SimpleAttributeSet(doc.getCharacterElement(start + i).getAttributes()); 
            if (StyleConstants.getIcon(attrs) == null) { 
               StyleConstants.setIcon(attrs, new new ImageIcon(ChatFrame.class.getResource("/smile.png"))); 

             doc.remove(start + i, 2); 
             doc.insertString(start + i, ":)", attrs); 

             StyleConstants.setIcon(attrs, new ImageIcon(ChatFrame.class.getResource("/spacer.png"))); 
             nameDoc.insertString(nameDoc.getLength()-6," ", attrs); //6 is "Martin" length 

            } 
            i = text.indexOf(":)", i + 2); 
           } 
         } catch (BadLocationException ex) { 
          ex.printStackTrace(); 
         } 
        } 
        }); 
       } 
      @Override 
      public void removeUpdate(DocumentEvent e) { 
      } 

      @Override 
      public void changedUpdate(DocumentEvent e) { 
      } 
     }); 
    } 
} 

smile.png http://postimage.org/image/vm7e4gvp1/ spacer.png http://postimage.org/image/k0q09iq6l/

+2

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

已發佈。我希望它不會太長。 – vejmartin

+0

+1不錯的SSCCE,抱歉我不知道如何解決 – mKorbel

回答

1

可能,最好使用一個主JTextPane(聊天)和多個獨立JTextPanes(或每個發送的消息均包含標籤)。然後,您可以控制單個消息標籤(或文本窗格),設置它們所需的高度。

可以通過將消息開始和結束偏移量傳遞給modelToView()方法並計算差異來計算高度。

+0

謝謝!我會研究它 – vejmartin

+0

@Martin :-)在嘗試之前不要解決問題,它真的起作用 – StanislavL

相關問題