我在JavaFX8中写了一个小时钟。在我的家伙,我有2个按钮。一个按钮应启动计时器,另一个按钮应暂停计时器。但每当我按下开始时,什么都没发生。当我删除整个if()子句时,我能够通过thread.start启动计时器并更新我的gui。我认为我的电话“if(isRunning)”并不是我想要的......:P 我感谢任何帮助!
My Window.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 gui;
import java.util.Observable;
import java.util.Observer;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import logic.Clock;
import logic.ClockObserver;
/**
*
* @author
*/
public class Window extends Application implements Observer {
private Button b_start = new Button("Start");
private Button b_stop = new Button("Stop");
private Label l_time = new Label("gdfgdf");
private HBox buttonbox = new HBox();
private Clock clock = new Clock();
private Thread thread = new Thread(clock);
@Override
public void start(Stage primaryStage) throws Exception {
thread.start();
buttonbox.setSpacing(5.0);
clock.addObserver(this);
buttonbox.setAlignment(Pos.CENTER);
buttonbox.getChildren().addAll(b_start, b_stop);
BorderPane bp = new BorderPane();
bp.setPadding(new Insets(10.0));
bp.setCenter(l_time);
bp.setBottom(buttonbox);
Scene scene = new Scene(bp);
primaryStage.setMinHeight(150);
primaryStage.setMinWidth(250);
primaryStage.setScene(scene);
primaryStage.setTitle("Uhr");
primaryStage.show();
b_start.setOnAction((ActionEvent e) ->{
clock.setRunning(true);
});
b_stop.setOnAction((ActionEvent e) ->{
clock.setRunning(false);
});
}
public static void main(String args[]) {
launch(args);
}
@Override
public void update(Observable o, Object o1) {
Platform.runLater(new Runnable(){
@Override
public void run() {
l_time.setText(clock.getZeit());
}
});
}
}
我的Clock.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 logic;
import gui.Window;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
/**
*
* @author
*/
public class Clock extends Observable implements Runnable {
private String zeit = "";
private int sek;
private boolean isRunning = false;
public Clock() {
}
public void setZeit() {
zeit = "" + sek;
}
public String getZeit() {
return zeit;
}
public void setRunning(boolean running){
this.isRunning = running;
}
public boolean isRunning(){
return isRunning;
}
public int getSek() {
return sek;
}
@Override
public void run() {
while (true) {
if (isRunning()) {
try {
sek++;
setZeit();
System.out.println(zeit);
this.setChanged();
this.notifyObservers();
Thread.sleep(1000);
} catch (InterruptedException ex) {
//TODO
}
}
}
}
}
提前致谢!