0

我是 Java 和 JavaFX 的新手,并且正在使用 Smack API 进行即时消息传递。目前,我有一个用于登录/注销 jabber 服务器(Openfire)、发送消息和接收消息的 java 文件。从 javafx 文件调用登录/注销和发送消息。但是,我正在努力让收到的消息显示在 javafx 中。我试图扩展 PacketListener 以从 java 调用回调,但我不确定这是否完全适用于类本身。我从这个网站得到了一些信息http://blog.crisp.se/perlundholm/2009/02/28/1235815701880.html

/*
 * stage.fx
*/


package unfc;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.stage.StageStyle;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.paint.*;
import javafx.scene.layout.VBox;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import unfc.accordion;
import javafx.scene.shape.Rectangle;
import unfc.xmpp.xmpp_main;
import javafx.scene.control.TextBox;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

class MucListener extends PacketListener {

public var mediator: xmpp_main;

override public function processPacket(packet: Packet) {

    if (packet instanceof Message) {
        var message: Message = packet as Message;


        FX.deferAction(function (): Void {

            mediator.xmpp_create_chat(message.getBody());

            });
    }
  }
}


public class stage {

public var getmsg: MucListener;
public var test: VBox;
public var test1: VBox;
public var test2: VBox;
public var message_new: TextBox;
public var uj = accordion{};
public var xmpp = xmpp_main{};
public var content: javafx.scene.Node;
public var content1: javafx.scene.Node;
public var content2: javafx.scene.Node;
public var testmsg = xmpp.msgArray;


public var bc = VBox {
            visible: true
            spacing: 10
            content: [

                test2 = VBox {
                    visible: true
                    width: 250
                    spacing: 10
                    content: [
                            Button {
                            text: "Button"
                            action: function () {xmpp.xmpp_connect();}
                            },
                    test = VBox {
                    visible: true
                    width: 250
                    spacing: 10
                    content: [content1,]

                    },
                    ]
                 },

                VBox {
                    visible: true
                    width: 250
                    spacing: 10

                    content: [
                            Button {
                            text: "Button1"
                            action: function () {xmpp.xmpp_disconnect();}
                            },
                    test1 = VBox {
                    visible: true
                    width: 250      
                    spacing: 10

                    content: [content2,

                    message_new = TextBox {

                             columns: 12
                             selectOnFocus: true
                                   },

                            Button {
                               text: "Send"
                               action: function ()   {xmpp.xmpp_create_chat(message_new.text); getmsg.mediator;}
                            }


                    ]},
                    ]
                 },
            ]
   }

public var layout = Stage {
title: "IM"
style: StageStyle.TRANSPARENT
resizable: false
scene: Scene {
    width: 500
    fill: Color.TRANSPARENT
    height: 400

    content: [

    Rectangle {

x:10 y:10 宽度:390 高度:250 arcWidth:30 arcHeight:30 不透明度:1.0 填充:LinearGradient { startX:0.0 startY:0.0 endX:0.0 endY:10.0 停止:[停止{颜色:Color.BLACK 偏移:0.0 }, 停止 { 颜色: Color.WHITE 偏移量: 1.0 }, ] } },

        VBox {
        visible: true
        content: bc

    }
    ]
  }
}

以下是 java 文件中创建聊天并发送消息以及启动消息侦听器的部分。

/*
 * xmpp_connections.java
 *
 */

package unfc.xmpp;

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import javafx.async.RunnableFuture;

/**
 * @author Chris
 */

public class xmpp_main {

.
.
.
.
.

    public void xmpp_create_chat(String msg) {

    ChatManager chatmanager = connection.getChatManager();
    Chat newChat = chatmanager.createChat("admin@127.0.0.1", new MessageListener() {

        @Override
        public void processMessage(Chat chat, Message message) {
            //msgArray.add( new String("Received message: " + message.getBody()) );
            msgArray = "Received message: " + message.getBody();
            System.out.println("Received message: " + message);



        }


    });

    try {
        newChat.sendMessage(msg);
    } catch (XMPPException e) {
        System.out.println("Error Delivering block");
    }
}

如果有人能指出我正确的方向,那就太好了。

4

1 回答 1

1

要将数据返回到 JavaFX 世界,您需要设置一个 Java 接口并让您的 JavaFX 类扩展该接口。但是,您只能在主事件线程上操作 JavaFX 对象。为此,您需要调用 JavaFX 入口类的 deferAction 方法。这是一个例子。

public void onMessage(final Message msg) {
    try {
            // must run this on the JavaFX Main thread
            // If you don't you will eventually get exceptions
            // in the JavaFX code.
            Entry.deferAction( new Runnable() {
                @Override
                public void run() {
                    fxListener.onMessage(msg);
                }
            } );

    } catch (Exception ex) {
            Logger.getLogger(Subscriber.class.getName()).log(Level.SEVERE, null, ex);
    }
}

下载第 12 章的代码示例 - JavaFX 代码配方,以获取基于 Java JMS 的更全面的示例。

其次,您不应该在主事件线程中进行连接等。如果连接需要很长时间,这将导致您的 gui 阻塞。最好在后台与Java连接代码进行交互。查看我的博客条目http://blogs.oracle.com/clarkeman/entry/javafx_async_task以查看如何执行此操作的示例。

于 2010-03-23T02:50:23.803 回答