如何在java swing中添加笑脸? [英] How to add smileys in java swing?
问题描述
我正在用 java swing 开发一个聊天应用程序,我想在其中添加笑脸,任何人都可以帮助我吗?
在 JEditorPane 中用适当的图像自动替换微笑文本
为了支持自动替换,我们需要一个带有
StyledEditorKit
(或扩展类)的JEditorPane
以提供文本图像.我们只需添加一个DocumentListener
来处理文本插入事件.插入后,我们检查更改后的文本是否包含微笑字符串——:)".如果它包含我们用适当的图像替换微笑文本.该示例仅提供一种微笑支持 - :)"字符串,但可以轻松扩展.
import javax.swing.*;导入 javax.swing.event.DocumentEvent;导入 javax.swing.event.DocumentListener;导入 javax.swing.text.*;导入 java.awt.image.BufferedImage;导入 java.awt.*;公共类 AutoreplaceSmiles 扩展 JEditorPane {静态 ImageIcon SMILE_IMG=createImage();公共静态无效主(字符串 [] args){JFrame frame = new JFrame("Autoreplace :) with Smiles images example");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);最终 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);}公共自动替换微笑(){极好的();}私人无效initListener(){getDocument().addDocumentListener(new DocumentListener(){公共无效插入更新(文档事件事件){最终 DocumentEvent e=event;SwingUtilities.invokeLater(new Runnable() {公共无效运行(){if (e.getDocument() instanceof StyledDocument) {尝试 {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(":)");而(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) {}});}静态 ImageIcon createImage() {BufferedImage res=new BufferedImage(17, 17, BufferedImage.TYPE_INT_ARGB);图形 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();返回新的 ImageIcon(res);}}
来源:http://java-sl.com/tip_autoreplace_smiles.html>
I am developing a chat application in java swing in which I would like to add smileys, can anyone help me with this?
Autoreplace smiles text with appropriate images in JEditorPane
To support the autoreplacing we need a
JEditorPane
withStyledEditorKit
(or extension class) to provide images in text. We just add aDocumentListener
to process text insert events. After inserting we check whether the changed text contains smiles string - the ":)". If it contains we replace the smile text with an appropriate image.The example provides only one smile support - the ":)" string but can be easy extended.
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);
}
}
Source: http://java-sl.com/tip_autoreplace_smiles.html
这篇关于如何在java swing中添加笑脸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!