如何在我的应用程序的文本字段中包含表情符号?

时间:2013-06-19 16:31:28

标签: java javascript

我正在尝试像java中的论坛一样开发问题和答案应用程序,我想在文本中添加表情符号。当我们在文本中输入符号时,如何获得表情符号?

1 个答案:

答案 0 :(得分:3)

用图像替换文字:

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

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import java.awt.image.BufferedImage;
import java.awt.*;

public class AutoreplaceSmiles extends JEditorPane {
    static ImageIcon SMILE_IMG=createImage();

    public static void main(String[] args) {
        JFrame frame = new JFrame("Autoreplace :) with Smiles images example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final AutoreplaceSmiles app = new AutoreplaceSmiles();
        app.setEditorKit(new StyledEditorKit());
        app.initListener();
        JScrollPane scroll = new JScrollPane(app);
        frame.getContentPane().add(scroll);

        frame.setSize(400, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public AutoreplaceSmiles() {
        super();
    }

    private void initListener() {
        getDocument().addDocumentListener(new DocumentListener(){
            public void insertUpdate(DocumentEvent event) {
                final DocumentEvent e=event;
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        if (e.getDocument() instanceof StyledDocument) {
                            try {
                                StyledDocument doc=(StyledDocument)e.getDocument();
                                int start= Utilities.getRowStart(AutoreplaceSmiles.this,Math.max(0,e.getOffset()-1));
                                int end=Utilities.getWordStart(AutoreplaceSmiles.this,e.getOffset()+e.getLength());
                                String text=doc.getText(start, end-start);

                                int i=text.indexOf(":)");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                        StyleConstants.setIcon(attrs, SMILE_IMG);
                                        doc.remove(start+i, 2);
                                        doc.insertString(start+i,":)", attrs);
                                    }
                                    i=text.indexOf(":)", i+2);
                                }
                            } catch (BadLocationException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                });
            }
            public void removeUpdate(DocumentEvent e) {
            }
            public void changedUpdate(DocumentEvent e) {
            }
        });
    }

    static ImageIcon createImage() {
        BufferedImage res=new BufferedImage(17, 17, BufferedImage.TYPE_INT_ARGB);
        Graphics g=res.getGraphics();
        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.yellow);
        g.fillOval(0,0,16,16);

        g.setColor(Color.black);
        g.drawOval(0,0,16,16);

        g.drawLine(4,5, 6,5);
        g.drawLine(4,6, 6,6);

        g.drawLine(11,5, 9,5);
        g.drawLine(11,6, 9,6);

        g.drawLine(4,10, 8,12);
        g.drawLine(8,12, 12,10);
        g.dispose();

        return new ImageIcon(res);
    }
}

http://onwebdev.blogspot.co.uk/2011/03/jquery-inserting-emoticons.html

var emoticons = {

  smile: '<img src="path/smile.gif" />',
  sad: '<img src="path/sad.gif" />',
  wink: '<img src="path/wink.gif" />'


};

var patterns = {

  smile: /:-\)/gm,
  sad: /:-\(/gm,
  wink: /;-\)/gm


};

$(document).ready(function() {


  $('p').each(function() {

    var $p = $(this);
    var html = $p.html();

    $p.html(html.replace(patterns.smile, emoticons.smile).
    replace(patterns.sad, emoticons.sad).
    replace(patterns.wink, emoticons.wink));  

  });
});

现场演示

http://dev.css-zibaldone.com/onwebdev/post/jquery-inserting-emoticons.html