Java Swing聊天HTML格式的消息

Java Swing聊天HTML格式的消息,java,swing,user-interface,jscrollpane,jlabel,Java,Swing,User Interface,Jscrollpane,Jlabel,我正在尝试使聊天客户端摇摆。我需要在通信历史中的信息是用HTML格式的 我试图用JTextPane解决这个问题,因为它支持HTML格式。当我这样做的时候,只是文本显示,原则上一切都正常 但是,当我使用HTML标记添加表情符号时,每次新消息出现时,通信窗口中的所有文本都开始抖动 我是如何做到的: jTextPane.setText ("message"); 当它是,新的信息,我这样做了 jTextPane.setText ("message" + "new message"); etc. 结果

我正在尝试使聊天客户端摇摆。我需要在通信历史中的信息是用HTML格式的

我试图用JTextPane解决这个问题,因为它支持HTML格式。当我这样做的时候,只是文本显示,原则上一切都正常

但是,当我使用HTML标记添加表情符号时,每次新消息出现时,通信窗口中的所有文本都开始抖动

我是如何做到的:

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>");
        }
    }
String[]split=text.split(“\t\t”);
字符串时间=拆分[0]。拆分(“\t”)[2]。拆分(“,”[1];
字符串发送方=拆分[0]。拆分(“\t”)[3];
字符串消息=拆分[1];
如果(!jTextPane.getText().equals(“请登录!”){
oldMsg=jTextPane.getText().substring(jTextPane.getText().indexOf(“”+6,jTextPane.getText().lastIndexOf(“”));
if(sender.equalsIgnoreCase(login.getText())){
msg=“”+checkmsgonmille(消息)++++++:“+checkSenderOnColor(发送者)+++”[“+time+”]”;
}否则{
msg=“[”+时间+”]“+”+支票寄件人颜色(寄件人)+:“+”“+”+支票寄件人微笑(信息);
}
字符串[]检查=(oldMsg+msg).split(
); 如果(检查长度>99){ ArrayList ArrayList=新的ArrayList(Arrays.asList(check)); arrayList.remove(0); 字符串str=“”; 对于(int i=0;i”; } jTextPane.setText(str); }否则{ oldMsg=oldMsg.replaceAll(“,”); oldMsg=oldMsg.replaceAll(“,”); setText(oldMsg+msg+“
”); } }

它可以被JLabel和JScrollPane替换吗?

如果您只需要显示具有不同颜色、字体等的文本,那么我发现使用文本和属性比使用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中。@Elstrix,为什么要使用JLabel?JLabel是为简单文本而设计的。JTextPane是为样式文本设计的。对不起,我没有完全检查所有内容!我认为JLabel还能够处理HTML标记以及JTextPane。告诉我,我如何优化您的代码,以便当新邮件到达时,它会逐渐出现在通信历史中?现在,当一条新消息到达时,它会变得非常不稳定。@elstrix,
我认为JLabel也能够处理HTML标记
——的确如此。如果要执行此操作,请使用垂直BoxLayout创建一个JPanel。然后创建标签并将其添加到面板中。然后需要调用revalidate()并重新绘制()
这样,当一条新消息到达时,它会逐渐出现在通信历史中?
-我不理解这条评论。每次调用insertString(…)方法时,文本都会添加到文本窗格中,因此每次客户端代码收到文本时都会逐渐添加文本。