gpt4 book ai didi

JavaFx8 属性绑定(bind)导致 "No Fx8 Application thread"

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:13 25 4
gpt4 key购买 nike

我的程序无法正常工作。我写了一个名为“Uhr”的类,它是一个简单的计时器。为了显示计时器,我还编写了一个名为“UhrMain”的类,它应该创建一个 GUI。一切正常,但每当我的 StringProperty 在类“Uhr”中发生更改时,我都会收到“No Fx8 Application thread”-错误。所以标签没有得到更新。

为了将时间绑定(bind)到标签,我使用了这段代码:

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);
}

}
}

}

主测试.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);
}

}

我感谢任何形式的帮助。

最佳答案

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

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

关于JavaFx8 属性绑定(bind)导致 "No Fx8 Application thread",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41831276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com