Java实现聊天发送表情代码
时间: 2023-11-12 20:05:33 浏览: 36
以下是一个简单的Java聊天程序,可以发送表情:
```java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class ChatClient extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JEditorPane chatPane;
private JTextField inputField;
private JButton sendButton, emojiButton;
private Map<String, ImageIcon> emojiMap;
public ChatClient() {
super("Chat Client");
chatPane = new JEditorPane();
chatPane.setEditable(false);
JScrollPane chatScroll = new JScrollPane(chatPane);
chatScroll.setPreferredSize(new Dimension(400, 300));
inputField = new JTextField(30);
sendButton = new JButton("Send");
sendButton.addActionListener(this);
emojiButton = new JButton("Emoji");
emojiButton.addActionListener(this);
JPanel inputPanel = new JPanel(new FlowLayout());
inputPanel.add(inputField);
inputPanel.add(sendButton);
inputPanel.add(emojiButton);
add(chatScroll, BorderLayout.CENTER);
add(inputPanel, BorderLayout.SOUTH);
initEmojiMap();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void initEmojiMap() {
emojiMap = new HashMap<String, ImageIcon>();
String[] emojiNames = {"smile", "cry", "angry", "cool"};
for (String name : emojiNames) {
try {
InputStream is = getClass().getResourceAsStream("/" + name + ".png");
Image image = ImageIO.read(is);
ImageIcon icon = new ImageIcon(image);
emojiMap.put(name, icon);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendButton) {
String text = inputField.getText();
appendText(text, null);
inputField.setText("");
} else if (e.getSource() == emojiButton) {
showEmojiDialog();
}
}
private void showEmojiDialog() {
JFrame emojiFrame = new JFrame("Select Emoji");
emojiFrame.setLayout(new FlowLayout());
for (Map.Entry<String, ImageIcon> entry : emojiMap.entrySet()) {
JLabel emojiLabel = new JLabel(entry.getValue());
emojiLabel.setName(entry.getKey());
emojiLabel.addMouseListener(new EmojiMouseListener());
emojiFrame.add(emojiLabel);
}
emojiFrame.pack();
emojiFrame.setVisible(true);
}
private void appendText(String text, ImageIcon icon) {
StyledDocument doc = chatPane.getStyledDocument();
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setForeground(attrs, chatPane.getForeground());
try {
if (icon != null) {
doc.insertString(doc.getLength(), " ", attrs);
chatPane.setCaretPosition(doc.getLength());
chatPane.insertIcon(icon);
chatPane.setCaretPosition(doc.getLength());
}
doc.insertString(doc.getLength(), text + "\n", attrs);
chatPane.setCaretPosition(doc.getLength());
} catch (BadLocationException e) {
e.printStackTrace();
}
}
private class EmojiMouseListener implements java.awt.event.MouseListener {
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
JLabel label = (JLabel) e.getSource();
String name = label.getName();
ImageIcon icon = emojiMap.get(name);
appendText(null, icon);
}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {}
}
public static void main(String[] args) {
new ChatClient();
}
}
```
在本例中,我们将表情图片作为资源文件打包在客户端中。程序中添加了一个Emoji按钮,当用户点击按钮时,弹出表情选择框。表情选择框中使用JLabel显示所有的表情图片,当用户选择表情时,程序会将表情插入到聊天框中。
注意:此代码仅作为示例,实际项目中需要根据需求进行调整和优化。