如何从不同的类中输入文本文件

时间:2012-05-26 15:34:46

标签: java swing jtextarea

我正在与发布商和订阅者进行聊天应用 我有两个一级的聊天框架,一个是聊天会员的班级。 成员类从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);
       }
    }
}

感谢。

1 个答案:

答案 0 :(得分: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();
        }
    }
}