2

我正在与发布者和订阅者一起制作聊天应用程序我有两个类,一个是聊天框架,一个是聊天成员的类。成员类从 jtextfield 获取消息,聊天成员发布者将其发送回 jtextarea。

我很难从订阅者 MessageListener 将文本返回到 jtextarea

@Override
public void onMessage(Message m)
{
    try
    {
        TextMessage textMessage = (TextMessage) m;
        System.out.println("Received:" + textMessage.getText());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

MessageListener listener = new MessageListener()
{
    @Override
    public void onMessage(Message m)
    {
        try
        {
            TextMessage textMessage = (TextMessage) m;
            System.out.println("Received:" + textMessage.getText());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
};

private void sendChatBattonActionPerformed(java.awt.event.ActionEvent evt) { 
   String empty=""; 
   String message= inputChatText.getText(); 
   if (message.equals(empty)){ 
       inputChatText.setText(""); 
       inputChatText.requestFocus(); 
   } else { 
       inputChatText.setText(""); 
       try { 
           String newMessage=userName+"~"+message+"~chat"; 
           sendMessage(newMessage); 
       } catch (JMSException ex) {
           Logger.getLogger(chatFrame.class.getName()).log(Level.SEVERE, null, ex);
       }
    }
}

谢谢。

4

1 回答 1

2

使用提供的代码片段,这就是我所理解的,要在文本区域中附加文本,请使用append.

@Override
public void onMessage(Message m) {
    try {
        TextMessage textMessage = (TextMessage) m;
        System.out.println("Received:" + textMessage.getText());
        jTextArea.apped(textMessage.getText());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

假设您的充当聊天框架的类被调用ChatFrame并具有MessageListener,在这种情况下您的代码将如下所示:

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.swing.JTextArea;


public class ChatFrame {
    JTextArea textArea = null;

    void init(){
        //create and show UI here
    }

    class MessageListener implements javax.jms.MessageListener {
        @Override
        public void onMessage(Message arg0) {
            TextMessage textMessage = (TextMessage)arg0;
            try {
                textArea.append(textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

第二种方法,您的ChatFrameMessageListener位于两个单独的 Java 源文件中,您的代码如下所示:

ChatFrame

import javax.swing.JTextArea;

public class ChatFrame {
    JTextArea textArea = null;

    void init(){
        //create and show UI here

        //Also start your message listener and give it the reference of textArea to be updated
        MessageListener messageListener = new MessageListener(this.textArea);
    }
}

MessageListener

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.swing.JTextArea;


public class MessageListener implements javax.jms.MessageListener {

    private JTextArea textArea = null;

    public MessageListener(JTextArea textArea){
        this.textArea = textArea;
        createConnection();
    }

    private void createConnection(){
        //Create your JMS Connection and add yourself as subscriber
    }

    @Override
    public void onMessage(Message arg0) {
        TextMessage textMessage = (TextMessage)arg0;
        try {
            textArea.append(textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
于 2012-05-26T15:41:19.613 回答