2010-02-18 21 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的编辑管理器