【问题标题】:JavaFx8 Properties Binding causing "No Fx8 Application thread"JavaFx8 属性绑定导致“无 Fx8 应用程序线程”
【发布时间】:2017-01-24 14:54:22
【问题描述】:

我的程序没有按我的意愿运行。我写了一个名为“Uhr”的类,它是一个简单的计时器。为了显示计时器,我还编写了一个名为“UhrMain”的类,它应该创建一个 GUI。一切都很完美,但是每当我的StringProperty 在“Uhr”类中更改时,我就会收到“No Fx8 Application thread”错误。所以标签没有更新。

为了将时间绑定到标签,我使用了以下代码:

timeLabel.textProperty().bind(eineUhr.zeitProperty());

我完全不知道为什么这不起作用。

Uhr.java:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uhr;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/**
 *
 * @author 
 */
public class Uhr extends Thread{

    private final StringProperty zeit;
    private int mSec;
    private int sec;
    private boolean running;

    public Uhr(){
        zeit = new SimpleStringProperty();
        running = false;
    }

    public final StringProperty zeitProperty(){
        return zeit;
    }

    public final String getZeit(){
        return zeit.get();
    }

    public synchronized void startTimer(){
        this.running = true;
    }

    public synchronized void stopTimer(){
        this.running = false;
    }

    @Override
    public void run(){
        while(true){
            try {
                Thread.sleep(100);
                mSec++;
                if(mSec >= 10){
                    mSec = 0;
                    sec++;
                    zeit.set("Zeit: " + sec);
                    System.out.println(zeit.get());
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(Uhr.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

}

MainTest.java

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uhr;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author
 */
public class UhrMain extends Application {

    private VBox box;
    private Button startButton;
    private Button stopButton;
    private Label timeLabel;
    private Uhr eineUhr;

    @Override
    public void start(Stage primaryStage) {
        eineUhr = new Uhr();
        eineUhr.start();
        box = new VBox();
        timeLabel = new Label("Zeit: ");
        startButton = new Button("Start");
        //startButton.setStyle("-fx-background-color: green");
        startButton.setOnAction((event) -> eineUhr.startTimer());
        stopButton = new Button("Stopp");
        //stopButton.setStyle("-fx-background-color: red");
        stopButton.setOnAction((event) -> eineUhr.stopTimer());
        box.getChildren().addAll(timeLabel,startButton,stopButton);
        timeLabel.textProperty().bind(eineUhr.zeitProperty());
        Scene scene = new Scene(box);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Stoppuhr");
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

感谢任何形式的帮助。

【问题讨论】:

  • 请注意running 从未在您的代码中使用,它只是设置而已
  • 是的,谢谢提及,已经注意到了。我认为这会导致问题,所以我暂时删除了操作。

标签: java multithreading javafx-8


【解决方案1】:

您应该将StringProperty 实例上的set 方法调用包装到Platform.runLater(Runnable runnable) 中,以确保通过JavaFX Application Thread 修改它来防止此问题,实际上调用set 将有直接影响您的 UI,因为它是绑定的,并且只有 JavaFX Application Thread 被允许修改您的 UI,因为 Java FX Components 不是线程安全的。

Platform.runLater(() -> zeit.set("Zeit: " + sec));

【讨论】:

  • 正要写同样的答案。必须在 UI 线程上完成对 StringProperty 的更改
【解决方案2】:

这个方法应该改一下:

@Override
public void run(){
    while(true){
        try {
            Thread.sleep(100);
            mSec++;
            if(mSec >= 10){
                mSec = 0;
                sec++;
                Platform.runLater(()->zeit.set("Zeit: " + sec));
                System.out.println(zeit.get());
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(Uhr.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

它为我解决了崩溃问题。

【讨论】:

  • 已经在我的启动方法中尝试过这个,但它不能正常工作。每当我在启动应用程序后调整窗口大小时,时间标签都会更新一次,不再更新。
  • 我已经改变了我的答案。应该有帮助。
  • 但是你能解释一下为什么我需要这样做吗?绑定通常在没有 Platform.runLater 的情况下工作,不是吗?
  • 因为 UI 的所有更改都应该在主线程中完成。使用 Platform.runLater() 将代码放入主线程的队列中,当应用程序有时间时,您的代码将被执行。
【解决方案3】:

我有一段时间没有使用 JavaFX,但我认为您只需要在正确的 UI 线程中运行任何与 UI 相关的东西(就像大多数 UI 框架一样,只能有一个线程来访问 UI 元素)。

Platform.runLater(new Runnable() {
  @Override public void run() {
    timeLabel.textProperty().bind(eineUhr.zeitProperty());   
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2021-08-07
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多