Java Smileys用笑脸替换字符串

时间:2014-11-08 17:35:00

标签: java jbutton chat jtextpane

我正在写一个聊天程序。我设计了一个带有表情符号的模拟gui,当用户点击笑脸(jbutton)时,它会将其打印到文本窗格上。我设法添加了一个高级功能,当用户输入“:)”并发送它时,它会使用insertIcon()方法插入笑脸而不是字符串。我遇到的问题是它只打印一次笑脸而不是多次。所以,如果我输入“嗨:)我的名字是杰克:)”它只插入图标ONCE。有什么建议吗?

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

public class SmileyTesterGUI extends JFrame {

    JPanel main = new JPanel();
    JPanel south = new JPanel();
    JPanel messageCenter = new JPanel();
    JPanel smileysNorth = new JPanel();
    JTextField text;
    JTextPane tPane;
    Icon happy;
    Icon smile;
    Icon tongue;
    Icon veryHappy;
    Icon wink;
    Icon laugh;
    Icon sad;
    Icon verySad;
    Icon cry;

    public SmileyTesterGUI() {
        super("Smileys");
        add(main);
        main.setLayout(new BorderLayout());
        main.add(south, BorderLayout.SOUTH);
        south.setLayout(new BorderLayout());
        south.add(messageCenter, BorderLayout.CENTER);
        south.add(smileysNorth, BorderLayout.NORTH);

        // textpane panel
        tPane = new JTextPane();
        JScrollPane sPane = new JScrollPane(tPane);
        main.add(sPane);
        tPane.setEditable(false);

        // smileysPanel
        smileysNorth.setLayout(new GridLayout(1, 0));
        JButton smiley1 = new JButton();
        JButton smiley2 = new JButton();
        JButton smiley3 = new JButton();
        JButton smiley4 = new JButton();
        JButton smiley5 = new JButton();
        JButton smiley6 = new JButton();
        JButton smiley7 = new JButton();
        JButton smiley8 = new JButton();
        JButton smiley9 = new JButton();
        smileysNorth.add(smiley1);
        smileysNorth.add(smiley2);
        smileysNorth.add(smiley3);
        smileysNorth.add(smiley4);
        smileysNorth.add(smiley5);
        smileysNorth.add(smiley6);
        smileysNorth.add(smiley7);
        smileysNorth.add(smiley8);
        smileysNorth.add(smiley9);

        // set smileys(icon) to each button - pathed from personal directory
        happy = new ImageIcon(getClass().getResource("smileys/Smile1.png"));
        smiley1.setIcon(happy);
        smile = new ImageIcon(getClass().getResource("smileys/Smile2.png"));
        smiley2.setIcon(smile);
        tongue = new ImageIcon(getClass().getResource("smileys/Smile3.png"));
        smiley3.setIcon(tongue);
        veryHappy = new ImageIcon(getClass().getResource("smileys/Smile4.png"));
        smiley4.setIcon(veryHappy);
        wink = new ImageIcon(getClass().getResource("smileys/Smile5.png"));
        smiley5.setIcon(wink);
        laugh = new ImageIcon(getClass().getResource("smileys/Smile6.png"));
        smiley6.setIcon(laugh);
        sad = new ImageIcon(getClass().getResource("smileys/Smile7.png"));
        smiley7.setIcon(sad);
        verySad = new ImageIcon(getClass().getResource("smileys/Smile8.png"));
        smiley8.setIcon(verySad);
        cry = new ImageIcon(getClass().getResource("smileys/Smile9.png"));
        smiley9.setIcon(cry);

        // smileys print on the textpane

        smiley1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tPane.insertIcon(new ImageIcon(getClass().getResource(
                        "smileys/Smile1.png")));
            }
        });
        smiley2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tPane.insertIcon(new ImageIcon(getClass().getResource(
                        "smileys/Smile2.png")));
            }
        });
        smiley3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tPane.insertIcon(new ImageIcon(getClass().getResource(
                        "smileys/Smile3.png")));
            }
        });
        smiley4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tPane.insertIcon(new ImageIcon(getClass().getResource(
                        "smileys/Smile4.png")));
            }
        });
        smiley5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tPane.insertIcon(new ImageIcon(getClass().getResource(
                        "smileys/Smile5.png")));
            }
        });
        smiley6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tPane.insertIcon(new ImageIcon(getClass().getResource(
                        "smileys/Smile6.png")));
            }
        });
        smiley7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tPane.insertIcon(new ImageIcon(getClass().getResource(
                        "smileys/Smile7.png")));
            }
        });
        smiley8.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tPane.insertIcon(new ImageIcon(getClass().getResource(
                        "smileys/Smile8.png")));
            }
        });
        smiley9.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                tPane.insertIcon(new ImageIcon(getClass().getResource(
                        "smileys/Smile9.png")));
            }
        });

        // messagePanel
        messageCenter.setLayout(new BorderLayout());
        text = new JTextField();
        JButton send = new JButton("Send");
        messageCenter.add(text);
        messageCenter.add(send, BorderLayout.EAST);

        text.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        send.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        setLocation(500, 0);
        setSize(600, 250);
    }

    public void sendMessage() {
        String a = text.getText();
        // tPane.setText(a);
        // tPane.getText();

        if (a.equals(":D")) {
            tPane.insertIcon(veryHappy);
        } else if (a.equals(":)")) {
            tPane.insertIcon(smile);
        } else if (a.equals(":(")) {
            tPane.insertIcon(sad);
        } else if (a.equalsIgnoreCase(":P")) {
            tPane.insertIcon(tongue);
        } else if (a.equals(";)")) {
            tPane.insertIcon(wink);
        }

        text.setText(null);
        text.requestFocus();

    }


    public static void main(String[] args) {
        new SmileyTesterGUI().setVisible(true);

    }

}

1 个答案:

答案 0 :(得分:0)

insertIcon()的{​​{1}}方法使用选择(最简单的插入位置)。所以在你的情况下,你总是只更换一次图标。

您的JTextPane未检查邮件中endMessage()的多次出现。使用while循环获取":)"的索引并为每个索引选择,然后使用":)"