java中发送者和接收者的不同聊天文本背景色

java中发送者和接收者的不同聊天文本背景色,java,netbeans,colors,chat,Java,Netbeans,Colors,Chat,我正在用Java NetBeans制作聊天信使。 它成功地工作了,但现在,我想区分发送者和接收者的文本背景颜色,以便用户能够轻松理解聊天对话。 我正在使用JTextArea显示聊天对话。 告诉我我在那里使用了什么技巧 这里我从谷歌搜索中得到了一些例子,但我不知道如何使用它 下面的类用于绘制第一个聊天气泡:(气泡左侧的箭头) 下面的代码是绘制第二个聊天泡泡。(气泡右侧的箭头): 下面是使用上述两个类的代码: 下面是这段代码的输出 帮助如何在我的项目中使用此背景样式。我想在jFrame中使用它,像

我正在用Java NetBeans制作聊天信使。
它成功地工作了,但现在,我想区分发送者和接收者的文本背景颜色,以便用户能够轻松理解聊天对话。
我正在使用
JTextArea
显示聊天对话。
告诉我我在那里使用了什么技巧

这里我从谷歌搜索中得到了一些例子,但我不知道如何使用它

下面的类用于绘制第一个聊天气泡:(气泡左侧的箭头) 下面的代码是绘制第二个聊天泡泡。(气泡右侧的箭头): 下面是使用上述两个类的代码: 下面是这段代码的输出


帮助如何在我的项目中使用此背景样式。我想在jFrame中使用它,像Viber、Facebook、Tango这样聊天。

JTextArea
用于输入和显示纯文本

应用于单个字符的设置适用于整个JTextArea

您可以使用JTextPane或JEditorPane编写彩色文本

有关如何在JTextPane中输入彩色文本的更多信息,请参见示例

要使用JEditorPane,请使用以下示例

我会选择
JTextPane
解决方案,因为它更简单


希望我能帮忙

没问题,但我也在使用append。。。如果我使用JTextPane或JEditorPane,因此无法使用append public void appendReceivedMessages(字符串s){this.txtReceivedMessages.append(s+“\n”);}是的。您的代码需要一些修改才能正常工作实际上我想要这样的样式我知道三星消息气泡样式对我来说很难,所以我想更改发送方和接收方的文本颜色。由于懒惰,可以在JTextPane/JEditorPane中使用(非严格的)HTML<代码>“

…”我并不懒惰,我只是初学者。我想学的越来越多。我在谷歌和stackoverflow上搜索,然后在这里发布,因为我需要在我的项目中使用它。

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JPanel;
/**
* @
*/
public class LeftArrowBubble extends JPanel {
   private static final long serialVersionUID = -5389178141802153305L;
   private int radius = 10;
   private int arrowSize = 12;
   private int strokeThickness = 3;
   private int padding = strokeThickness / 2;
   @Override
protected void paintComponent(final Graphics g) {
  final Graphics2D g2d = (Graphics2D) g;
  g2d.setColor(new Color(0.5f, 0.8f, 1f));
  int x = padding + strokeThickness + arrowSize;
  int width = getWidth() - arrowSize - (strokeThickness * 2);
  int bottomLineY = getHeight() - strokeThickness;
  g2d.fillRect(x, padding, width, bottomLineY);
  g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,   RenderingHints.VALUE_ANTIALIAS_ON));
  g2d.setStroke(new BasicStroke(strokeThickness));
  RoundRectangle2D.Double rect = new RoundRectangle2D.Double(x, padding, width, bottomLineY, radius, radius);
  Polygon arrow = new Polygon();
  arrow.addPoint(20, 8);
  arrow.addPoint(0, 10);
  arrow.addPoint(20, 12);
  Area area = new Area(rect);
  area.add(new Area(arrow));
  g2d.draw(area);
    }
}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JPanel;
/**
* @author
*/
  public class RightArrowBubble extends JPanel {
   private static final long serialVersionUID = -5389178141802153305L;
   private int strokeThickness = 3;
   private int radius = 10;
   private int arrowSize = 12;
   private int padding = strokeThickness / 2;
  @Override
  protected void paintComponent(final Graphics g) {
  final Graphics2D g2d = (Graphics2D) g;
  g2d.setColor(new Color(0.5f, 0.5f, 1f));
  int bottomLineY = getHeight() - strokeThickness;
  int width = getWidth() - arrowSize - (strokeThickness * 2);
  g2d.fillRect(padding, padding, width, bottomLineY);
  RoundRectangle2D.Double rect = new RoundRectangle2D.Double(padding, padding, width, bottomLineY,  radius, radius);
  Polygon arrow = new Polygon();
  arrow.addPoint(width, 8);
  arrow.addPoint(width + arrowSize, 10);
  arrow.addPoint(width, 12);
  Area area = new Area(rect);
  area.add(new Area(arrow));
  g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  g2d.setStroke(new BasicStroke(strokeThickness));
  g2d.draw(area);
   }
 }
import java.awt.HeadlessException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
 * @author harsh
 */
public class TestPanel extends JPanel {
private static final long serialVersionUID = 9029457020704524363L;
private JLabel messageLbl1, userImageLbl1, messageLbl, userImageLbl;
private JPanel msgPanel1, msgPanel;
String userImageUrl = "http://cdn1.iconfinder.com/data/icons/nuvola2/22x22/apps/personal.png";
public TestPanel() throws MalformedURLException {
    userImageLbl = new JLabel();
    msgPanel = new LeftArrowBubble();
    messageLbl = new JLabel();
    messageLbl1 = new JLabel();
    msgPanel1 = new RightArrowBubble();
    userImageLbl1 = new JLabel();

    userImageLbl.setIcon(new ImageIcon(new URL(userImageUrl)));
    messageLbl.setText("Hi, How are you?");

    GroupLayout msgPanelLayout = new GroupLayout(msgPanel);
    msgPanel.setLayout(msgPanelLayout);
    msgPanelLayout.setHorizontalGroup(
        msgPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(msgPanelLayout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addComponent(messageLbl)
            .addContainerGap(162, Short.MAX_VALUE))
    );
    msgPanelLayout.setVerticalGroup(
        msgPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(msgPanelLayout.createSequentialGroup()
            .addComponent(messageLbl)
            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    messageLbl1.setIcon(new ImageIcon(new URL(userImageUrl)));
    userImageLbl1.setText("I'm Good.");

    GroupLayout jPanel1Layout = new GroupLayout(msgPanel1);
    msgPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(171, Short.MAX_VALUE)
            .addComponent(userImageLbl1)
            .addGap(22, 22, 22))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addComponent(userImageLbl1)
            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    GroupLayout layout = new GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(userImageLbl)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                .addComponent(msgPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(msgPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(messageLbl1)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(userImageLbl)
                .addComponent(msgPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(messageLbl1)
                .addComponent(msgPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
            .addContainerGap(22, Short.MAX_VALUE))
    );
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                JOptionPane.showMessageDialog(null, new TestPanel());
            } catch (HeadlessException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
      });
    }
}