2014-11-08 108 views
0

我正在寫一個聊天程序。我設計了一個帶笑臉的模擬gui,當用戶點擊一個笑臉(jbutton)時,它將它打印到文本面板上。我設法添加一個高級功能,當用戶鍵入「:)」併發送它時,它使用insertIcon()方法插入笑臉而不是字符串。我遇到的問題是它只打印一次笑臉而不是多次。所以,如果我輸入「嗨:)我的名字是傑克:)」它只插入圖標一次。有什麼建議麼?Java笑臉替代字符串笑臉

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); 

    } 

} 

回答

0

insertIcon()JTextPane方法使用選擇(插入位置在簡單的情況下)。所以在你的情況下,你總是隻替換一次圖標。

您的endMessage()不檢查消息中的多個出現":)"。使用while循環獲取":)"的索引,併爲每個索引使其選中,然後使用insertIcon()