JavaFX使用Platform.runLater从另一个线程更新TextArea

JavaFX使用Platform.runLater从另一个线程更新TextArea,java,multithreading,user-interface,javafx,Java,Multithreading,User Interface,Javafx,我正在尝试从另一个实现Runnable的类中使用Platform.runLater更新TextArea。 我的所有GUI都在一个类中(我的TextArea就在这个类中),我正在创建一个新服务器线程,并在创建GUI时运行它。我正在尝试使用平台。runLater从服务器线程更新我的文本区域但是平台。runLater无法到达我的文本区域 public class SimulationWindow { public SimulationWindow instance() { re

我正在尝试从另一个实现
Runnable
的类中使用
Platform.runLater
更新TextArea。 我的所有GUI都在一个类中(我的TextArea就在这个类中),我正在创建一个
新服务器
线程,并在创建GUI时运行它。我正在尝试使用
平台。runLater
服务器
线程更新我的
文本区域
但是
平台。runLater
无法到达我的文本区域

public class SimulationWindow {
    public SimulationWindow instance() {
        return this;
    }
    public static void DisplaySimulationWindow() throws FileNotFoundException {
        Stage SimuStage = new Stage();
        SimuStage.initModality(Modality.APPLICATION_MODAL);
        SimuStage.setTitle("Simulation Window");
        Server myServer = new Server(instance());
        Thread serverThread = new Thread(myServer);
        serverThread.start();
        TextArea serverTextArea;
         .
         .
         .
}

public class Server implements Runnable {
    @Override
    public void run() {
        while(true){
            whileConnected();
        .
        .
    }
    private void whileConnected() throws IOException {

        sendMessage(message);

        do {
            try {
                message = (String) input.readObject();  
                showMessage(message);
                .
                .
                .
    }
   private void showMessage(String x) {
    Platform.runLater(() -> serverTextArea.appendText(x));          
   }
我尝试将SimulationWindow的实例传递给服务器构造函数,如下所示:

但是Java不会让我的SimulationWindow实例作为服务器构造函数的参数传递。 其他解决方案将hold Server和SimulationWindow类作为一个类,但我希望将它们分开。
任何提示都将不胜感激

我按照克利奥帕特拉的建议解决了这个问题。我向我的服务器构造函数传递了一个TextArea。我想从我的GUI类以及从服务器类的客户机获取消息时更新GUI中的TextArea。(我在SimuWindow()中创建并启动服务器)


我在我的客户机类上也做了同样的操作。感谢您的帮助。

在服务器中,添加一个以textArea作为参数的构造函数“但是Java不会让我的SimulationWindow实例作为服务器构造函数的参数传递。”什么
SimulationWindow
实例:您的代码中没有。
displaySimulationWindow()
真的需要是
static
?嗨@James\u D我在问题中添加了实例方法。谢谢你指出这一点。我删除了static,现在可以将实例发送到服务器构造函数,但仍然无法从服务器中访问serverTextArea。您希望一个类如何访问另一个类的方法本地成员?正如您所看到的,代码片段相当无用;)嗨@kleopatra谢谢你的提示,我正试着按照你的建议通过一个文本区。是的,我需要学会小心处理片段:P
public class myGUI{
    public void SimuWindow(){
        //this method creates all the GUI.
        Server myServer = new Server(serverTextArea);
        Thread serverThread = new Thread(myServer);
        serverThread.start();

        sendingTest = new TextField();
        sendingTest.setPromptText("test communication here");
        sendingTest.setOnAction(event -> {
        String message = new String ("\nServer says: ");
        message = message + sendingTest.getText();
        serverTextArea.appendText(message);
        myServer.sendMessage(message);
        sendingTest.clear();
    });
    }
}

public class Server implements Runnable{
    //This is my server class that connects and listens to clients
    TextArea mytextArea;
    public Server(TextArea x) {
        this.mytextArea = x;
    }
    private void whileConnected() throws IOException {          
    do {
        try {
            message = (String) input.readObject();  
            showMessage(message);               
        }catch(ClassNotFoundException classNotFoundException) {
            System.out.println("\n i dont know the message");
        }
    }while(!message.equals("Disconnected"));    

    private void showMessage(String mess) {
            Platform.runLater(() -> mytextArea.appendText(mess));           
    }
}