2010-02-18 68 views
-1

我想用java編寫一個messenger應用程序。我想發送笑臉照片。我寫了這段代碼,但它不起作用。有關java任務的幫助

public class MyClient extends JFrame implements IClient { 

...

JEditorPane editorPane=new JEditorPane(); 
final String SMILE = ClassLoader.getSystemClassLoader().getResource("images/1.gif").toString(); 

...

public void chat(String message) { 
     try { 
      StringBuffer bfr= new StringBuffer(message); 

      while(message.indexOf(":)") != -1) { 
       int index = message.indexOf(":)"); 
       bfr.replace(index,index+2,"<IMG SRC=\""+SMILE +"\">"); 
       message= bfr.toString(); 

      } 

try{ 
      editorPane.getEditorKit().read(new java.io.StringReader(message+"\n"), 
        editorPane.getDocument(), editorPane.getDocument().getLength()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (BadLocationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    editorPane.setCaretPosition(editorPane.getDocument().getLength()); 
    } 

... 但desnt顯示興高采烈的editorpane,輸出爲: 這是與該興高采烈的地址爲被保存。 我投這樣的代碼:

(HTMLEditorKit)editorPane.getEditorKit().read(new java.io.StringReader(message+"\n"), 
        editorPane.getDocument(), editorPane.getDocument().getLength()); 

但它會導致異常,因爲這是投不正確。所以我能做什麼? 感謝

回答

0

我看到兩個可能性:

  1. 你定義一個控制字符串(如在MSN),其中:)變成笑臉GIF(這一個接收器必須具備GIF過 - 所以我去包含所有的GIF圖像文件夾)
  2. 你必須閱讀的gif文件,文件的內容傳送到誰,然後顯示它(這是比較難的解決方案)
0

確定接收器,我找到了答案。我剛分配HTMLEditorKit作爲我的JEditorPane的編輯管理器