将输出写入不同类中的文本区域,而不是使用 system.out.println

发布于 2024-10-26 03:14:19 字数 12588 浏览 1 评论 0原文

我有一个名为 readGUI 的 GUI 类和一个名为 TopicS 的侦听器类,

其目的是在位于的 textArea 中打印 TopicS 的输出readGUI

我不知道如何执行此操作,但我尝试附加文本区域,即。

在 GUI 类中,我添加了

     public void receiveMsg (String s)
    { jTextArea1.append(s);
    }

while in listner classs Topics I added the two lines after the commented out System.out.printlm(messageText)

 public void onMessage(Message message)
 {
  try
  {
   String messageText = null;
   if (message instanceof TextMessage)
    messageText = ((TextMessage)message).getText();
  // System.out.println(messageText);
readGUI rm = readGUI();
rm.receiveMsg(messageText);
  }
  catch (JMSException e)
  {
   e.printStackTrace();
  }
 }

Listner 打印发布到正确主题的消息,但我不想将结果放在控制台上,而是将结果放在我从

以下两个完整类调用的 GUI 上(如果有帮助的话) readGUI

    public class readGUI extends javax.swing.JFrame {

    /** Creates new form readMessages */
    public readGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        topicCombobox = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        listenme = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Listen for specific messages");
        // String [] tlist = { "topic1", "WGB1", "WGB2", "Item 4" };
        topicCombobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "topic1", "WGB1", "WGB2", "Item 4" }));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel2.setText("Location:");

        listenme.setText("Listen");
        listenme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Get complete log");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Sensor");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Location");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(jLabel2)
                .addGap(30, 30, 30)
                .addComponent(topicCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
                .addComponent(listenme, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46))
            .addGroup(layout.createSequentialGroup()
                .addGap(219, 219, 219)
                .addComponent(jLabel1)
                .addContainerGap(239, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 609, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(19, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(listenme)
                    .addComponent(topicCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.out.println("test test test"); 
        System.out.print("you pressed" +topicCombobox.getSelectedItem());
        //TopicPublish tp = new TopicPublish();
       TopicS a = new TopicS();
 a.addTopicToListner((String) topicCombobox.getSelectedItem());
//int i = topicCombobox.getSelectedIndex();
//a.addTopicToListner(tlist[i]);
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new readGUI().setVisible(true);
            }
        });

}
    public void receiveMsg (String s)
{jTextArea1.append(s);
}
    // Variables declaration - do not modify
    private javax.swing.JButton listenme;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox topicCombobox;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    // End of variables declaration

}

主题

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicS implements MessageListener
{

 private TopicConnection topicConnection;
 private TopicSession topicSession;
 public Topic topic;
 private TopicSubscriber topicSubscriber;


 public TopicS()
            {}
            public void addTopicToListner(String t){
  try
  {
   // create a JNDI context
   Hashtable properties = new Hashtable();
   properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.exolab.jms.jndi.InitialContextFactory");
   properties.put(Context.PROVIDER_URL,"rmi://localhost:1099/");
   Context context = new InitialContext(properties);

   // retrieve topic connection factory
   TopicConnectionFactory topicConnectionFactory = 
       (TopicConnectionFactory)context.lookup("JmsTopicConnectionFactory");
   // create a topic connection
   topicConnection = topicConnectionFactory.createTopicConnection();

   // create a topic session
   // set transactions to false and set auto acknowledgement of receipt of messages
   topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);

   // retrieve topic
   topic = (Topic) context.lookup(t);

   // create a topic subscriber and associate to the retrieved topic
   topicSubscriber = topicSession.createSubscriber(topic);

   // associate message listener
   topicSubscriber.setMessageListener(this);

   // start delivery of incoming messages
   topicConnection.start();
  }
  catch (NamingException e)
  {
   e.printStackTrace();
  }
  catch (JMSException e)
  {
   e.printStackTrace();
  }
 } 

/* public static void main(String[] args)
 //{

  try
  {
   TopicS listener = new TopicS();
   Thread.currentThread().sleep(2000);
  }

  catch (InterruptedException e)
  {
   e.printStackTrace();
  }
 }
 */
 // process incoming topic messages
 public void onMessage(Message message)
 {
  try
  {
   String messageText = null;
   if (message instanceof TextMessage)
    messageText = ((TextMessage)message).getText();
  // System.out.println(messageText);
readGUI rm = readGUI();
rm.receiveMsg(messageText);
  }
  catch (JMSException e)
  {
   e.printStackTrace();
  }
 }
}

I have a GUI class called readGUI and a listener class called TopicS

The aim is to print the output from TopicS in a textArea which is located in readGUI

I am not sure how to do this but I tried to append textarea ie.

in GUI class I added

     public void receiveMsg (String s)
    { jTextArea1.append(s);
    }

while in listner classs Topics I added the two lines after the commented out System.out.printlm(messageText)

 public void onMessage(Message message)
 {
  try
  {
   String messageText = null;
   if (message instanceof TextMessage)
    messageText = ((TextMessage)message).getText();
  // System.out.println(messageText);
readGUI rm = readGUI();
rm.receiveMsg(messageText);
  }
  catch (JMSException e)
  {
   e.printStackTrace();
  }
 }

The Listner prints message that are published to the correct topic's, but instead of on the console I would like to put results on GUI I called from

The two full classes are below if it helps
readGUI

    public class readGUI extends javax.swing.JFrame {

    /** Creates new form readMessages */
    public readGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        topicCombobox = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        listenme = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Listen for specific messages");
        // String [] tlist = { "topic1", "WGB1", "WGB2", "Item 4" };
        topicCombobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "topic1", "WGB1", "WGB2", "Item 4" }));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel2.setText("Location:");

        listenme.setText("Listen");
        listenme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Get complete log");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Sensor");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Location");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(jLabel2)
                .addGap(30, 30, 30)
                .addComponent(topicCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
                .addComponent(listenme, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46))
            .addGroup(layout.createSequentialGroup()
                .addGap(219, 219, 219)
                .addComponent(jLabel1)
                .addContainerGap(239, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 609, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(19, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(listenme)
                    .addComponent(topicCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.out.println("test test test"); 
        System.out.print("you pressed" +topicCombobox.getSelectedItem());
        //TopicPublish tp = new TopicPublish();
       TopicS a = new TopicS();
 a.addTopicToListner((String) topicCombobox.getSelectedItem());
//int i = topicCombobox.getSelectedIndex();
//a.addTopicToListner(tlist[i]);
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new readGUI().setVisible(true);
            }
        });

}
    public void receiveMsg (String s)
{jTextArea1.append(s);
}
    // Variables declaration - do not modify
    private javax.swing.JButton listenme;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox topicCombobox;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    // End of variables declaration

}

TopicS

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicS implements MessageListener
{

 private TopicConnection topicConnection;
 private TopicSession topicSession;
 public Topic topic;
 private TopicSubscriber topicSubscriber;


 public TopicS()
            {}
            public void addTopicToListner(String t){
  try
  {
   // create a JNDI context
   Hashtable properties = new Hashtable();
   properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.exolab.jms.jndi.InitialContextFactory");
   properties.put(Context.PROVIDER_URL,"rmi://localhost:1099/");
   Context context = new InitialContext(properties);

   // retrieve topic connection factory
   TopicConnectionFactory topicConnectionFactory = 
       (TopicConnectionFactory)context.lookup("JmsTopicConnectionFactory");
   // create a topic connection
   topicConnection = topicConnectionFactory.createTopicConnection();

   // create a topic session
   // set transactions to false and set auto acknowledgement of receipt of messages
   topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);

   // retrieve topic
   topic = (Topic) context.lookup(t);

   // create a topic subscriber and associate to the retrieved topic
   topicSubscriber = topicSession.createSubscriber(topic);

   // associate message listener
   topicSubscriber.setMessageListener(this);

   // start delivery of incoming messages
   topicConnection.start();
  }
  catch (NamingException e)
  {
   e.printStackTrace();
  }
  catch (JMSException e)
  {
   e.printStackTrace();
  }
 } 

/* public static void main(String[] args)
 //{

  try
  {
   TopicS listener = new TopicS();
   Thread.currentThread().sleep(2000);
  }

  catch (InterruptedException e)
  {
   e.printStackTrace();
  }
 }
 */
 // process incoming topic messages
 public void onMessage(Message message)
 {
  try
  {
   String messageText = null;
   if (message instanceof TextMessage)
    messageText = ((TextMessage)message).getText();
  // System.out.println(messageText);
readGUI rm = readGUI();
rm.receiveMsg(messageText);
  }
  catch (JMSException e)
  {
   e.printStackTrace();
  }
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-11-02 03:14:19

如果您要将文本附加到组件,并且该工作不是在 gui 线程上完成,您将会遇到问题。这似乎就是你在这里所做的。使用

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
         rm.receiveMsg(messageText);
    }
});

以便该工作在 gui 线程上完成。

我还想知道你在做什么:

readGUI rm = readGUI();

看起来你正在尝试调用构造函数而不创建对象或其他东西。

If you are appending text to a component and that work is being done NOT on the gui thread you will have problems. That appears to be what you are doing here. Use

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
         rm.receiveMsg(messageText);
    }
});

so that that work is done on the gui thread.

I am also wondering what you are doing with this:

readGUI rm = readGUI();

it seems like you are trying to call a constructor without creating an object or something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文