我可以使用线程在JavaFX中设置标签文本吗?

时间:2019-02-26 17:45:01

标签: java multithreading javafx nullpointerexception

我想不显示任何按钮就不断显示汽车的当前行驶里程。

线程类

package threads;

import javafx.fxml.FXML;
import javafx.scene.control.Label;

import static controller.GameWindowController.*;

public class MileageThread implements Runnable {


@FXML
Label mileageLabel;

@Override
public void run() {
    {
        double time = 1;
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (NullPointerException | InterruptedException e) {
                e.printStackTrace();
            }
            getCar().setDistancePerSec((time * getCar().getCurrentCarSpeed()) / 3600);
            getCar().setCarMileage(getCar().getCarMileage() + getCar().getDistancePerSec());
            System.out.format("dziala %.3f km przebiegu %n", getCar().getCarMileage());
            mileageLabel.setText(String.valueOf(getCar().getCarMileage()));
        }
    }
}

}

通过此记录,我收到:

Exception in thread "Thread-5" java.lang.NullPointerException
at threads.MileageThread.run(MileageThread.java:27)

这是创建线程的类:

package controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import model.VehicleStatus;
import threads.MileageThread;
import vehicles.Car;

import java.net.URL;
import java.util.ResourceBundle;

public class GameWindowController extends Thread implements Initializable, Runnable {

@FXML
ImageView smoke;

@FXML
private Label speedLabel, mileageLabel;

static Car car;

static {
    car = new Car() {
    };
}

public void accelerationButtonPushed(ActionEvent event) throws InterruptedException {
    car.accelerate();

    speedLabel.setText(String.valueOf(car.getCurrentCarSpeed()));

    smoke.setFitHeight(smoke.getX() + car.getCurrentCarSpeed());
    smoke.setFitWidth(smoke.getY() + car.getCurrentCarSpeed());

    System.out.println(car.getCurrentCarSpeed());
    System.out.println(car.getCarMileage());
    System.out.println(car.getFuel());
    System.out.println(car.getStatus());
}

public void stopCarButtonPushed(ActionEvent event) throws InterruptedException {

    if (car.getCurrentCarSpeed() == 0) {
        System.out.println("Silnik zgasl!");
    }
    car.stop();

    speedLabel.setText(String.valueOf(car.getCurrentCarSpeed()));
    smoke.setFitHeight(smoke.getX() + car.getCurrentCarSpeed());
    smoke.setFitWidth(smoke.getY() + car.getCurrentCarSpeed());

    System.out.println(car.getCurrentCarSpeed());
    System.out.println(car.getCarMileage());
    System.out.println(car.getFuel());
    System.out.println(car.getStatus());
}

public void startButtonPushed(ActionEvent event) throws InterruptedException {
    if (car.getStatus() != VehicleStatus.MOVING) {
        car.start();
    } else System.out.println("Samochod juz jest na chodzie!");

    System.out.println("Status pojazdu: " + car.getStatus());
}

@FXML
public void initialize(URL location, ResourceBundle resources) {
    smoke.setFitHeight(1);
    smoke.setFitWidth(1);

    MileageThread mileageThread = new MileageThread();
    Thread mt = new Thread(mileageThread);
    mt.start();

}

public static Car getCar() {
    return car;
}

}

如果您在这里发现一些基本错误,或者我误解了您,我深表歉意。

我正在使用场景构建器,我只想使用MileageThread将文本放在标签之一中。

2 个答案:

答案 0 :(得分:3)

如果要从JavaFX Application线程以外的其他线程修改用户界面,则必须将其包装在对Platform.runLater()的调用中

Platform.runLater(() -> {
    userInterfaceControl.setText("New Text");
});

JavaFX节点/控件/等。只能从JavaFX Application线程进行修改。

答案 1 :(得分:1)

最适合我的解决方案:

@FXML
    public void initialize(URL location, ResourceBundle resources) {
        PauseTransition wait = new PauseTransition(Duration.seconds(1));
    wait.setOnFinished((e) -> {
        mileageLabel.setText(String.valueOf(getCar().getCarMileage()));
        wait.playFromStart();
    });
    wait.play();
}

我希望有人会帮助:)