我尝试将TextArea更改为TextFlow,以便在聊天应用程序中显示彩色文本。使用TextArea,此代码可以正常工作。但是使用Textflow,我收到了异常消息。
public void initialize()throws Exception{
(new Thread() {
public void run() {
isClient = true;
client = new Client(ip,userName,port){
public void display(CommandList command, String msg){
switch(command) {
case WELCOME:
textAreaMessages.getChildren().add(new Text("Welcome to SAND, "+userName+"\n"));
break;
}
}
};
}
}).start();
我也尝试使用Platform.runlater,但它也没有用。
public void initialize()throws Exception{
Platform.runLater(new Runnable() {
public void run() {
client = new Client(ip,userName,port){
public void display(CommandList command, String msg){
switch(command) {
case WELCOME:
textAreaMessages.getChildren().add(new Text("Welcome to SAND, "+userName+"\n"));
//enable text-input in chat
break;
case MESSAGE:
textAreaMessages.getChildren().add(new Text(msg+"\n"));
break;
}
}
};
}
});
}
这是我收到的异常消息。
Exception in thread "Thread-5" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.Parent$2.onProposedChange(Parent.java:367)
at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at ClientController$1$1.display(ClientController.java:47)
at Client$2.run(Client.java:167)