问题 如何在java swing中添加表情符号?


我正在开发一个java swing中的聊天应用程序,我想在其中添加表情符号,任何人都可以帮我这个吗?


10775
2018-03-16 03:21


起源

你需要比这更具体......除非你对像这样的解决方案感到满意 System.out.print(":-)");。 - Stephen C
好吧,在gtalk或yahoo messengers中的集体表情? - harishtps


答案:


http://java-sl.com/tip_autoreplace_smiles.html


6
2018-03-16 06:10



对不起StanislavL,我不记得我从哪里复制了那段代码。我将网站引用添加到我的源代码副本中。 - camickr
谢谢。我认为你的代码略有不同。设置另一个图像和另一个触发器。最好创建一次图像并传递引用而不是重新创建它。但绝对两种方式都有效:) - StanislavL


这是我很久以前在网上发现的一些简单代码。我真的不喜欢那是使用插入符听众。您应该使用DocumentListener或DocumentFilter。但它会让您了解如何使用自定义图标来表示笑脸。

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class Smiley
    extends JFrame {
    //autoreplacing :) with picture
    JTextPane p = new JTextPane();
    public Smiley() throws Exception {
        p.setEditorKit(new StyledEditorKit());
        getContentPane().add(p, BorderLayout.CENTER);
        SimpleAttributeSet attrs = new SimpleAttributeSet();
        StyleConstants.setIcon(attrs, getImage());
        p.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            StyledDocument doc = (StyledDocument) p.getDocument();
                            String text = doc.getText(0, p.getDocument().getLength());
                            int index = text.indexOf(":)");
                            int start = 0;
                            while (index > -1) {
                                Element el = doc.getCharacterElement(index);
                                if (StyleConstants.getIcon(el.getAttributes()) == null) {
                                    doc.remove(index, 2);
                                    SimpleAttributeSet attrs = new SimpleAttributeSet();
                                    StyleConstants.setIcon(attrs, getImage());
                                    doc.insertString(index, ":)", attrs);
                                }
                                start = index + 2;
                                index = text.indexOf(":)", start);
                            }
                        }
                        catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            }
        });
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400, 400);
    }

    public static void main(String[] args) throws Exception {
        Smiley test11 = new Smiley();
        test11.show();
    }

    protected ImageIcon getImage() {
        BufferedImage bi = new BufferedImage(15, 15, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.getGraphics();
        g.setColor(Color.red);
        g.drawOval(0, 0, 14, 14);
        g.drawLine(4, 9, 9, 9);
        g.drawOval(4, 4, 1, 1);
        g.drawOval(10, 4, 1, 1);
        return new ImageIcon(bi);
    }
}

7
2018-03-16 03:32



您应该接受StanislavL的答案,因为他是该代码的原作者。 - camickr


您可以复制这些Unicode字符并使用它们:☻

在Java字符串中,这些将是 "\u263a" 和 "\u263b"


3
2018-03-16 03:24



那个没有单独的罐子吗? - harishtps
@harishtps:没有JAR;只是一个包含所需字形的字体。这是一个 例。 - trashgod


import java.awt.*;
public class SmileyFace {

   public static void main(String[] args){


        Frame f = new Frame("Smile Face");
        f.setSize(500, 500);
        f.setVisible(true);

        Graphics g;
        g = f.getGraphics();


        while (true)
        {
            g.setColor(Color.black);
            g.drawOval(100, 100, 100, 100);
            g.setColor(Color.blue);
            g.fillOval(120, 130, 20, 20);
            g.fillOval(160, 130, 20, 20);
            g.setColor(Color.blue);
            g.setColor(Color.red);
            g.drawLine(130, 170, 135, 175);
            g.drawLine(135, 175, 163, 175);
            g.drawLine(163, 175, 168, 170);
            g.setColor(Color.green);
            g.drawString("Hello", 210, 190);



        }


    }
}

-1
2017-07-06 15:06



可爱............ - Walrus the Cat