【问题标题】:Swing Chat HTML formatted messagesSwing Chat HTML 格式的消息
【发布时间】:2015-02-25 04:49:12
【问题描述】:

我正在尝试为 Swing 创建一个聊天客户端。我需要那封邮件历史上的通信都用 HTML 格式。

我尝试使用 JTextPane 解决的问题,因为它支持 HTML 格式。我做的时候,只是文字显示,原则上一切正常。

但是当我使用HTML标签<img>添加表情符号时,每次有新消息时,通信窗口中的所有文本都开始抽搐。

我是怎么做到的:

jTextPane.setText ("message");

当它是新消息时,我这样做了

jTextPane.setText ("message" + "new message"); etc.

结果就是俄罗斯方块的“蛇”原理。结果,我不喜欢它的工作方式。

所以请告诉我是否可以使用 JLabel 将新消息添加到 JScrollpane 来推断它们?如何让每一个新帖子都是一个独立的元素?

    String[] split = text.split("\t\t");

    String time = split[0].split("\t")[2].split(", ")[1];
    String sender = split[0].split("\t")[3];
    String message = split[1];

    if (!jTextPane.getText().equals("Please log in!")) {
        oldMsg = jTextPane.getText().substring(jTextPane.getText().indexOf("<body>") + 6, jTextPane.getText().lastIndexOf("</body>"));

        if(sender.equalsIgnoreCase(login.getText())) {
            msg = "<div style=\"text-align:right\">" + checkMsgOnSmile(message) + " " + "<b>" + " :" + checkSenderOnColor(sender) + "</b>" + "<span style=\"font-size:10pt\">[" + time + "]</span></div>";
        } else {
            msg = "<div style=\"\"><span style=\"font-size:10pt\">[" + time + "]</span> " + "<b>" + checkSenderOnColor(sender) + ":" + "</b>" + " " + checkMsgOnSmile(message);
        }

        String[] check = (oldMsg + msg).split("<br>");
        if (check.length > 99) {
            ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(check));
            arrayList.remove(0);
            String str = "";

            for (int i = 0; i < arrayList.size(); i++) {
                str = str + arrayList.get(i).toString() + "<br>";
            }

            jTextPane.setText(str);
        } else {
            oldMsg = oldMsg.replaceAll("<span><font size=\"10pt\">", "<span style=\"font-size:10pt\">");
            oldMsg = oldMsg.replaceAll("</font></span>", "</span>");
            jTextPane.setText(oldMsg + msg + "<br>");
        }
    }

可以用 JLabel 和 JScrollPane 代替吗?

【问题讨论】:

    标签: java swing user-interface jscrollpane jlabel


    【解决方案1】:

    如果您只需要显示具有不同颜色、字体等的文本,那么我发现使用文本和属性比使用 HTML 更容易。一个简单的例子是这样的代码:

    JTextPane textPane = new JTextPane();
    textPane.setText( "Hello:" );
    textPane.setEditable(false);
    StyledDocument doc = textPane.getStyledDocument();
    
    //  Define a keyword attribute
    
    Simple AttributeSet keyWord = new SimpleAttributeSet();
    StyleConstants.setForeground(keyWord, Color.RED);
    StyleConstants.setBackground(keyWord, Color.YELLOW);
    StyleConstants.setBold(keyWord, true);
    
    //  Add some text
    
    try
    {
        doc.insertString(doc.getLength(), "\nAnother line of text", keyWord );
    }
    catch(Exception e) {}
    

    【讨论】:

    • 我想摆脱 JTextPane。并使用 JLabel 显示新消息。我可以这样做,以便当新消息到达时,创建一个新的 JLabel 实例,它会放置一条新消息,然后将此 JLabel 添加到 JScrollPane 中。
    • @Elsrix,你为什么要使用 JLabel? JLabel 专为简单文本而设计。 JTextPane 专为样式文本而设计。
    • 对不起,我没有完全检查一切!我认为 JLabel 也能够处理 HTML 标签,以及 JTextPane。告诉我,我如何优化你的代码,以便当有新消息到达时,它会逐渐出现在通信历史中?现在当有新消息到达时非常不稳定。
    • @Elsrix, I thought that the JLabel, is also able to handle HTML tags - 是的。如果您想这样做,请使用垂直 BoxLayout 创建一个 JPanel。然后创建标签并将其添加到面板中。然后您需要调用 revalidate() 和 repaint()。 so that when a new message arrives, it gradually appeared in the history of correspondence? - 我不明白评论。每次调用 insertString(...) 方法时,文本都会添加到文本窗格中,因此每次客户端代码收到文本时都会逐渐完成。
    猜你喜欢
    • 2014-08-18
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多