我是 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");
}
}
如果有人能指出我正确的方向,那就太好了。