【问题标题】:help for a java assignmentjava作业的帮助
【发布时间】:2011-01-18 06:20:47
【问题描述】:

我想用java写一个messenger应用程序。我想发送笑脸图片。我已经写了这段代码,但它不起作用。

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());
    }

… 但它不会在编辑器窗格中显示笑脸,输出是: 这是保存笑脸的地址。 我这样转换代码:

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

但它会导致异常,因为此转换不正确。那我能做什么? 谢谢

【问题讨论】:

    标签: java variable-assignment


    【解决方案1】:

    好的,我找到了答案。我刚刚将 HTMLEditorKit 分配为我的 JEditorPane 的编辑器包

    【讨论】:

      【解决方案2】:

      我看到了两种可能性:

      1. 您定义了一个控制字符串(如在 msn 中),其中 :) 成为笑脸 gif(对于这个,接收者也必须有 gif - 所以我会选择一个包含所有 gif 的图像文件夹)
      2. 您必须读取 gif 文件并将文件内容传输给接收方,然后接收方才能显示(这是更难的解决方案)

      【讨论】:

        猜你喜欢
        • 2022-06-13
        • 2023-03-24
        • 1970-01-01
        • 2011-08-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-26
        • 1970-01-01
        相关资源
        最近更新 更多