数字时钟不会更新

时间:2016-07-03 18:01:40

标签: javafx javafx-8 fxml

我试图将数字时钟集成到标签中,在FXML控制器中,但它不起作用,它不会更新,只显示标签的原始文本(dunno)

FXML:     

import java.net.URL;
import java.util.Calendar;
import java.util.ResourceBundle;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.util.Duration;

/**
 *
 * @author Antonio
 */
public class ControllerPannello implements Initializable {
    @FXML
    private ChoiceBox settimana;
    @FXML
    private Label orario, boh;
    @FXML
    private RadioButton onRadio, offRadio;

    private ObservableList<String> sett = 
            FXCollections.<String>observableArrayList("Lunedì", "Martedì", "Mercoledì",
                 "Giovedì", "Venerdì", "Sabato", "Domenica");   
    ToggleGroup stato = new ToggleGroup();

    @FXML
    private void handleButtonAction(ActionEvent event) {

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        onRadio.setToggleGroup(stato);
        offRadio.setToggleGroup(stato);
        settimana.setItems(sett);

        orario = new DigitalClock();




    }    

}

Fxml控制器:

import java.io.IOException;
import java.net.URL;
import javafx.animation.*;
import javafx.event.*;
import javafx.scene.control.Label;
import javafx.util.Duration;

import java.util.Calendar;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;



class DigitalClock extends Label {
  public DigitalClock() {
    bindToTime();
  }

  // the digital clock updates once a second.
public void bindToTime() {
    Timeline timeline = new Timeline(
      new KeyFrame(Duration.seconds(0),
        new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent actionEvent) {
            Calendar time = Calendar.getInstance();
            String hourString = StringUtilities.pad(2, ' ', time.get(Calendar.HOUR) == 0 ? "12" : time.get(Calendar.HOUR) + "");
            String minuteString = StringUtilities.pad(2, '0', time.get(Calendar.MINUTE) + "");
            String secondString = StringUtilities.pad(2, '0', time.get(Calendar.SECOND) + "");
            String ampmString = time.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
            setText(hourString + ":" + minuteString + ":" + secondString + " " + ampmString);
          }
        }
      ),
      new KeyFrame(Duration.seconds(1))
    );
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();
  }
}

class StringUtilities {
  /**
   * Creates a string left padded to the specified width with the supplied padding character.
   * @param fieldWidth the length of the resultant padded string.
   * @param padChar a character to use for padding the string.
   * @param s the string to be padded.
   * @return the padded string.
   */
  public static String pad(int fieldWidth, char padChar, String s) {
    StringBuilder sb = new StringBuilder();
    for (int i = s.length(); i < fieldWidth; i++) {
      sb.append(padChar);
    }
    sb.append(s);

    return sb.toString();
  }
}

DigitalClock.java

404.php

0 个答案:

没有答案