2016-12-18 2 views
0

私はJavaでクロックプログラムを開発していますが、私は時間更新を第2に行う方法がわかりません。私はthread.sleep(1000);とforループを使ってみましたが、うまくいきませんでした。また、誰かが白で開いてから少し遅れて黒に変わるのを止める方法が分かっていれば、本当に感謝しています。あなたがセットアップTimerTaskをしなければならないし、別のthead要素でそれを実行JavaFXのシーンで時間を細かく更新する必要があります

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 
import javafx.scene.paint.Color; 
import javafx.scene.layout.HBox; 
import javafx.scene.text.Font; 
import javafx.scene.text.Text; 
import java.text.DateFormat; 
import java.util.Date; 
import java.text.SimpleDateFormat; 

    public class Clock extends Application { 

     @Override public void start(Stage stage) { 

      DateFormat df = new SimpleDateFormat("EEE,MMM d yyyy - h:mm:ss a"); 
      Date date = new Date(); 
      String stringDate = df.format(date); 

      Text text = new Text(10, 60, stringDate); 
      text.setFont(Font.font ("Digital Dream Fat", 30f)); 
      text.setFill(Color.RED); 

      HBox hbox = new HBox(); 

      Scene scene = new Scene(new Group(text)); 
      scene.setFill(Color.BLACK); 

      stage.setScene(scene); 
      stage.initStyle(StageStyle.UNDECORATED); 
      stage.setWidth(710); 
      stage.setHeight(80); 
      stage.show(); 
     } 

     public static void main(String[] args) { 
      launch(args); 
     } 
    } 

答えて

0

まず第一に、あなたはUIのコンポーネントはスレッドセーフではありませんので、UI上の任意の更新操作は、UIのスレッド自体の内部ではなく、バックグラウンドスレッドから行われなければならないことを忘れてはいけませんそれ以外の場合は、UIのスレッドをブロックします。

これを更新するには、スレッドセーフなメカニズムを使用できます。その1つはTimelineクラスです。これをコードに実装しましょう。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 
import javafx.scene.paint.Color; 
import javafx.scene.layout.HBox; 
import javafx.scene.text.Font; 
import javafx.scene.text.Text; 
import java.text.DateFormat; 
import java.util.Date; 
import java.text.SimpleDateFormat; 
import javafx.animation.Animation; 
import javafx.animation.KeyFrame; 
import javafx.animation.Timeline; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.util.Duration; 

    public class Clock extends Application { 

     @Override public void start(Stage stage) { 

      DateFormat df = new SimpleDateFormat("EEE,MMM d yyyy - h:mm:ss a"); 
      Date date = new Date(); 
      String stringDate = df.format(date); 

      Text text = new Text(10, 60, stringDate); 
      text.setFont(Font.font ("Digital Dream Fat", 30f)); 
      text.setFill(Color.RED); 

      HBox hbox = new HBox(); 


      Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0), 
     new EventHandler<ActionEvent>() { 

      @Override 
      public void handle(ActionEvent actionEvent) { 
       Date update = new Date(); 
      String stringNewDate = df.format(update); 
        text.setText(stringNewDate); 
      } 
     } 

     ), new KeyFrame(Duration.seconds(1))); 
       timeline.setCycleCount(Animation.INDEFINITE); 
     timeline.play(); // timeline.stop() 

      Scene scene = new Scene(new Group(text)); 
      scene.setFill(Color.BLACK); 

      stage.setScene(scene); 
      stage.initStyle(StageStyle.UNDECORATED); 
      stage.setWidth(710); 
      stage.setHeight(80); 
      stage.show(); 
     } 

     public static void main(String[] args) { 
      launch(args); 
     } 
    } 

スリープ/休止部分は、この行に制御されている。.. new KeyFrame(Duration.seconds(1)))ので、毎分は、それだけでタイムラインのインスタンスを取る停止するには、Duration.minutes(1)になりますが、その後そんなにtimeline.stop()

+0

感謝を行います。これはとても役に立ちました – Herseept72

+0

あなたは大歓迎です、それを楽しんでください:P –

関連する問題