当前位置: 首页 > 面试题库 >

Javafx在使用计时器时不在fx应用程序线程上

巫马瀚漠
2023-03-14
问题内容

我正在用这个

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;



public class Main extends Application {

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

@Override
public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 600, 400);
    primaryStage.setScene(scene);
    Circle circle = new Circle(300,200,50, Color.BLACK);
    primaryStage.setTitle("Circle");
    primaryStage.setResizable(false);
    root.getChildren().add(circle);
    moveCircle(circle, scene);
    primaryStage.show();
}
public int random(int min, int max) {
    return new Random().nextInt((max - min) + min);
}

public int random(int max) {
    return random(0, max);
}

public void moveCircle(Circle circle, Scene scene) {
    Platform.runLater(() -> {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                circle.setCenterX(random((int) scene.getX()));
                circle.setCenterY(random((int) scene.getY()));

            }
        }, 1000, 1000);
    });
}

但是这个:

public void moveCircle(Circle circle, Scene scene) {
    Platform.runLater(() -> {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                circle.setCenterX(random((int) scene.getX()));
                circle.setCenterY(random((int) scene.getY()));

            }
        }, 1000, 1000);
    });
}

给我这个错误:

Exception in thread "Timer-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:204)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:364)
    at javafx.scene.Scene.addToDirtyList(Scene.java:485)
    at javafx.scene.Node.addToSceneDirtyList(Node.java:424)
    at javafx.scene.Node.impl_markDirty(Node.java:415)
    at javafx.scene.shape.Shape.impl_markDirty(Shape.java:942)
    at javafx.scene.shape.Circle$1.invalidated(Circle.java:136)
    at javafx.beans.property.DoublePropertyBase.markInvalid(DoublePropertyBase.java:112)
    at javafx.beans.property.DoublePropertyBase.set(DoublePropertyBase.java:146)
    at javafx.scene.shape.Circle.setCenterX(Circle.java:122)
    at Main$2.run(Main.java:48)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

而且我真的不明白怎么了


问题答案:

可能是因为您误解了Platform.runLater()工作原理。

正确的代码段为:

public void moveCircle(Circle circle, Scene scene) {
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            Platform.runLater(() -> {
                circle.setCenterX(random((int) scene.getX()));
                circle.setCenterY(random((int) scene.getY()));
            });
        }
    }, 1000, 1000);
}

但:

我强烈建议您不要使用TimerTimeLine而是使用!它是JavaFX
API的一部分,您不必执行这些Platform.runLater()调用。这只是很快就被黑了,但是您知道了:

public void moveCircle(Circle circle, Scene scene) {
    Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), ev -> {
        circle.setCenterX(random((int) scene.getX()));
        circle.setCenterY(random((int) scene.getY()));
    }));
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();
}


 类似资料:
  • 问题内容: 我试图通过使用JInternalFrame附加JFXPanel来使用JavaFx 2.x和Swing应用程序 我的代码如下 我有这个例外 就我的目的而言,我必须使用JInternalFrame:如何解决此问题? 问题答案: 请参阅“ Swing中的JavaFX”教程。您正在执行应在Swing线程(事件调度线程)上的JavaFX线程上运行的JavaFX操作。 幸运的是,他们从以前的错误中

  • 我正试图通过使用一个JInternalFrame来使用JavaFx2.x和Swing应用程序,其中附加了一个JFXPanel 我在下面的代码 我有这个例外 对于我的目的,我必须使用JInternalFrame:我如何解决这个问题?

  • 下面的代码片段给我错误< code >不在FX应用程序线程上;当前线程 = < code > Java FX < code >应用程序线程。这个应用程序在java 1.7中运行良好,但是当我把它移到fx8时,它现在出现错误。当我第一次尝试启动应用程序时,它按预期工作。但是在关闭并再次打开舞台后,它不起作用了。 错误也不明确。如果当前线程是fx应用程序线程,那么不在fx应用线程上意味着什么。

  • 问题内容: 我正在尝试从线程中设置文本对象的字符串,但这给了我这个错误: 处理程序类: 我尝试使用,它确实可以工作,但是它使我的程序崩溃。我也尝试在该方法上创建一个Timer,但它给了我与以前相同的错误。 问题答案: 包起来。在它的外面,在while循环中,添加Thread.sleep(1000); 非法状态异常的原因是你试图在JavaFX Application线程以外的其他线程上更新UI。 添

  • 我试图显示一个计时器,它在点击开始按钮后计数并在标签中显示秒数,但我没有得到想要的输出。这是我的控制器代码和FXML文件。。。 这是我的FXML代码...

  • 在过去的几天里,我一直在尝试JavaFX、FXML、任务和属性。我偶然发现了一种奇怪的行为,希望您能帮助我更好地理解正在发生的事情。 我有一个极简的GUI,看起来像这样:GUI 如果我单击按钮,就会创建并启动一个新任务。此任务会增加一个双属性,并且新值会写入标签并在ProgressBar中设置。任务代码可以在这里看到: FXML控制器的代码如下: 如果我运行此代码,尝试更新标签会导致以下异常:ja