【发布时间】: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