首页 > 解决方案 > 在线程中刷新 Java FX 窗口

问题描述

我有客户端-服务器应用程序,客户端有两个线程,一个用于向服务器发送信息,另一个用于显示正在更改的窗口,但出现错误。我读过一些关于 Platform.runlater 的东西,但我不知道如何使用它。这是带有窗口的类:

import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class B implements Runnable{

private int counter = 0, lg;
private double x_1, x_2, y_1, y_2;
int [] bts = new int[2];
Button[] change_bts = new Button[2];
Button [] all_bts = new Button[221];

public void run() {

    Group grupa = new Group();
    int x = 0; 

    int ctr = 1;


    for (int i = 0; i < 17; ++i) {
        for (int j = 0; j < 13; ++j) {
            if ( i%2 == 1)  {  x = 30; }
            else { x = 0; }

            Button bt = new Button();
            bt.setLayoutX(x+30+j*60); bt.setLayoutY(30+i*60);

            bt.setOnAction(
                    event -> { 
                        if((bt.getText()!="0")) {
                            if ((counter == 1)) {
                                change_bts[counter] = bt;
                                bts[0] = Integer.parseInt(change_bts[0].getText());
                                bts[1] = Integer.parseInt(change_bts[1].getText());
                            }
                            else if (counter == 0) {
                                change_bts[counter] = bt;
                                counter++;
                            }
                        }
                        System.out.println(bt.getText());
                    }
            );
            all_bts[ctr-1] =  bt;
            ctr ++;
        }
    }
    grupa.getChildren().addAll(all_bts);
    Scene tsc = new Scene(grupa, 1100, 1200);
    Stage tst = new Stage(); //New window (Stage)
    tst.setScene(tsc);
    tst.show();
}
}

这是创建新线程的类:

import javafx.application.Application;
import javafx.stage.Stage;
import java.io.IOException;

public class Clients extends Application {

public static void main(String[] args) {  launch(args);}

public void start(final Stage first_stage) throws IOException, InterruptedException {
   Names n = new Names (); // this Class' constructor calls gogo() method below
}
public static void gogo(String a) {
    Runnable[] runners = new Runnable[2];
    Thread[] threads = new Thread[2];

        runners[0] = new B();
        runners[1] = new Another_Class();

        threads[0] = new Thread(runners[0]);
        threads[1] = new Thread(runners[1]);

        threads[0].start();
        threads[1].start();
}
}

请帮忙:<

标签: javajavafxclient-serverjava-threads

解决方案


考虑将客户端所需的信息封装在一个单独的类(通常称为模型)中。
客户端(视图)应该响应模型的变化。在以下示例中,它通过绑定来实现。
现在您可以使用线程来更新模型:

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Clients extends Application {

    public static void main(String[] args) {  launch(args);}

    @Override
    public void start(final Stage first_stage) throws IOException, InterruptedException {

        Model model = new Model();
        Button run = new Button("Run");
        run.setOnAction(e -> {
            gogo(model);
            run.setDisable(true);
        });

        Label counter = new Label();
        counter.textProperty().bind(model.getCounterProperty().asString());

        BorderPane root = new BorderPane(counter);
        root.setBottom(run);
        Scene scene = new Scene(root);
        first_stage.setScene(scene);
        first_stage.show();
    }

    private void gogo(Model model) {
        //thread manipulates model 
        new Thread(new Updater(model)).start();
    }
}

class Updater implements Runnable{

    private final Model model;
    private int counter = 0;
    private boolean stop = false;

    public Updater(Model model) {
        this.model = model;
    }

    @Override
    public void run() {
        while (! stop){
            model.setCounter(++counter);
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
            } catch (InterruptedException ex) { ex.printStackTrace();}
        }
    }

    void setStop(boolean stop) {this.stop = stop;   }
}

//use a model to encapsulate information needed for the view 
class Model {

    private final SimpleIntegerProperty counterProperty = new SimpleIntegerProperty(0);

    SimpleIntegerProperty getCounterProperty() { return counterProperty; }

    //use `Platform.runLater` to allow update from non javafx thread 
    //if updated by more than one thread it needs to be synchronized 
    void setCounter(int value) {
        Platform.runLater(() -> counterProperty.set(value) );
    }
}

推荐阅读