gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:24:05 25 4
gpt4 key购买 nike

我想在不点击任何按钮等情况下不断显示汽车的当前里程

线程类

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 将文本放入其中一个标签中。

最佳答案

如果您想从 JavaFX 应用程序线程以外的线程修改用户界面,您必须将其包装在对 Platform.runLater() 的调用中

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

JavaFX 节点/控件/等。只能从 JavaFX 应用程序线程修改。

关于java - 我可以使用线程在 JavaFX 中设置标签文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54891313/

25 4 0