我的程序无法正常运行。我编写了一个名为“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);
}
}
}
}
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);
}
}
我非常感谢您的帮助。
最佳答案
此方法应更改:
@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);
}
}
}
它为我解决了崩溃。