Java 如何使用JTextPane在客户端和服务器之间进行彩色编码的聊天?

Java 如何使用JTextPane在客户端和服务器之间进行彩色编码的聊天?,java,swing,jtextpane,Java,Swing,Jtextpane,我有一个简单的客户机-服务器聊天系统,我想对其进行颜色编码,以便来自客户机的消息和来自服务器的消息以不同的颜色显示。我有以下资料: try { String messageout=""; messageout=jTextField1.getText(); jTextField1.setText(""); appendToPane(jTextPane1,"\n"+"client: "+message

我有一个简单的客户机-服务器聊天系统,我想对其进行颜色编码,以便来自客户机的消息和来自服务器的消息以不同的颜色显示。我有以下资料:

       try {
            String messageout="";
           messageout=jTextField1.getText();
           jTextField1.setText("");

        appendToPane(jTextPane1,"\n"+"client: "+messageout,Color.BLUE);
        dos.writeUTF(messageout);
    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }
dos是数据输出流

以及:

}

服务器具有将颜色设置为绿色而不是蓝色的simlar代码。问题是,我希望客户端消息以蓝色显示,服务器消息以绿色显示,而目前,在客户端,所有消息都以蓝色显示,在服务器上,所有消息都以绿色显示。我希望: '客户:blaablaa(蓝色)' '服务器:blaablaa(绿色)'

有人能帮忙吗

编辑:客户端从服务器读取(在找到真正的解决方案之前,颜色已删除)

我想在最后一行代码中添加颜色上下文

编辑-使用appendToPane而不是setText(当我从appendToPane中删除最后一个setText时,不显示任何内容:

    ss = new ServerSocket(1000);
    s = ss.accept();
    dis=new DataInputStream(s.getInputStream());
    dos=new DataOutputStream(s.getOutputStream());
    while(!msgin.equals("bye")){
        msgin=dis.readUTF();
         appendToPane(jTextPane1,"\n"+"client: "+msgin,Color.RED); 
以及:

您可以使用您的Swing组件

b1 = new JButton("<html><center><b><u>D</u>isable</b><br>"
                 + "<font color=#ffffdd>middle button</font>",
                 leftButtonIcon);
Font font = b1.getFont().deriveFont(Font.PLAIN);
b1.setFont(font);
...
b2 = new JButton("middle button", middleButtonIcon);
b2.setFont(font);
b2.setForeground(new Color(0xffffdd));
...
b3 = new JButton("<html><center><b><u>E</u>nable</b><br>"
                 + "<font color=#ffffdd>middle button</font>",
                 rightButtonIcon);
b3.setFont(font);
b1=新的JButton(“禁用
” +“中键”, 左按钮(尼康); Font Font=b1.getFont().deriveFont(Font.PLAIN); b1.设置字体(字体); ... b2=新的JButton(“中间按钮”,中间按钮图标); b2.设置字体(字体); b2.设置前景(新颜色(0xffffdd)); ... b3=新的JButton(“启用
” +“中键”, 右按钮(尼康); b3.setFont(字体);

代码取自链接页面。

如果文本窗格设置为不可编辑(例如,您的某个地方有一个tp.setEditable(false),则您不能使用编辑它的操作。方法replaceSelection()是一种编辑方法,因此,它不做任何事情,只会发出嘟嘟声

因此,您选择替换文本窗格的整个文本,这不被视为一种编辑方法。但是,您会丢失样式

在不可编辑的文本窗格中,您应该添加到支持文本窗格的文档中,而不是执行这两种操作。因此,例如,更改
附录窗格
,如下所示:

private static void appendToPane(JTextPane tp, String msg, Color c)
{
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

    aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
    aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

    // Get the TextPane's Document
    Document doc = tp.getDocument();
    int len = doc.getLength();
    try {
        doc.insertString(len, msg, aset);  // Use the `insertString` method of the document.
    } catch (BadLocationException e) {
        // Nothing. Using the doc length makes sure this exception isn't thrown
    }

}

将消息传递到客户的“代码”>“JTTXPANE < /Code >?@ RealSistpTIC.CODE。添加到哪里?”颜色被删除,直到找到解决方案。Gilbert Le Blanc——这正是问题所在。我如何向从服务器/ Client中输入的消息添加颜色?注意到您的代码> AddoPope< /Code > DISPL。将每条消息延迟两次。为什么要使用
tp.setText(tp.getText()+msg)
最后?如果没有它,您的方法似乎可以正常工作,并允许文本的不同部分使用不同的颜色。@Real疑论如果没有它,文本窗格中不会显示任何内容。您能想想为什么吗?我不想将HTML添加到按钮或组件本身。我想在组件中的文本中添加它。我不再怀疑论谢谢你,非常感谢!
        String messageout="";
        messageout=jTextField1.getText();


    jTextField1.setText("");
     appendToPane(jTextPane1,"\n"+"server:"+messageout,Color.BLUE);
     //jTextPane1.setText(jTextPane1.getText()+"\n"+"server:"+messageout);
        dos.writeUTF(messageout);
b1 = new JButton("<html><center><b><u>D</u>isable</b><br>"
                 + "<font color=#ffffdd>middle button</font>",
                 leftButtonIcon);
Font font = b1.getFont().deriveFont(Font.PLAIN);
b1.setFont(font);
...
b2 = new JButton("middle button", middleButtonIcon);
b2.setFont(font);
b2.setForeground(new Color(0xffffdd));
...
b3 = new JButton("<html><center><b><u>E</u>nable</b><br>"
                 + "<font color=#ffffdd>middle button</font>",
                 rightButtonIcon);
b3.setFont(font);
private static void appendToPane(JTextPane tp, String msg, Color c)
{
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

    aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
    aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

    // Get the TextPane's Document
    Document doc = tp.getDocument();
    int len = doc.getLength();
    try {
        doc.insertString(len, msg, aset);  // Use the `insertString` method of the document.
    } catch (BadLocationException e) {
        // Nothing. Using the doc length makes sure this exception isn't thrown
    }

}