###前提・実現したいこと
JavaFX,FXMLアプリケーションでGUIデジタル時計をanimation.Timelineを使って作成したい。
###発生している問題・エラーメッセージ
起動時に時計の自動スタートの仕方がわからない。
###該当のソースコード
Java
1package timelineの実験; 2 3import java.net.URL; 4import java.time.LocalTime; 5import java.util.ResourceBundle; 6import javafx.event.ActionEvent; 7import javafx.fxml.FXML; 8import javafx.fxml.Initializable; 9import javafx.scene.control.Label; 10import javafx.animation.Timeline; 11import javafx.animation.KeyFrame; 12import javafx.util.Duration; 13import javafx.event.*; 14/** 15 * 16 * @author test 17 */ 18public class FXMLDocumentController implements Initializable { 19 20 @FXML 21 private Label label; 22 23 @FXML 24 private void handleButtonAction(ActionEvent event) { 25 Timeline timeline = new Timeline(new KeyFrame(Duration.millis(1000), 26 new EventHandler<ActionEvent>() { 27 @Override 28 public void handle(ActionEvent actionEvent) { 29 LocalTime now=LocalTime.now(); 30 String txt=String.format("%02d:%02d:%02d",now.getHour(),now.getMinute(),now.getSecond()); 31 label.setText(txt); 32 } 33 } 34 )); 35 timeline.setCycleCount(Timeline.INDEFINITE); 36 timeline.play(); 37 } 38 39 @Override 40 public void initialize(URL url, ResourceBundle rb) { 41 // TODO 42 43 LocalTime now=LocalTime.now(); 44 String txt=String.format("%02d:%02d:%02d",now.getHour(),now.getMinute(),now.getSecond()); 45 label.setText(txt); 46 } 47 48}
java
1package timelineの実験; 2 3import javafx.application.Application; 4import javafx.fxml.FXMLLoader; 5import javafx.scene.Parent; 6import javafx.scene.Scene; 7import javafx.stage.Stage; 8 9/** 10 * 11 * @author test 12 */ 13public class Timelineの実験 extends Application { 14 15 @Override 16 public void start(Stage stage) throws Exception { 17 Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 18 19 Scene scene = new Scene(root); 20 21 stage.setScene(scene); 22 stage.show(); 23 } 24 25 /** 26 * @param args the command line arguments 27 */ 28 public static void main(String[] args) { 29 launch(args); 30 } 31 32} 33
XML
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.text.*?> 4<?import java.lang.*?> 5<?import java.util.*?> 6<?import javafx.scene.*?> 7<?import javafx.scene.control.*?> 8<?import javafx.scene.layout.*?> 9 10<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="timelineの研究.FXMLDocumentController"> 11 <children> 12 <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" /> 13 <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69"> 14 <font> 15 <Font size="28.0" /> 16 </font></Label> 17 </children> 18</AnchorPane>
###試したこと
Java初心者です。
がんばってソースコードを書いたのですが、このコードだとボタンを押さないとTimelineのイベントが始まりません。
それを起動時に自動的に時計の進行が始まるよう修正したいと思っています。
色々コンストラクタのようなものを試してみたり、 public void start(Stage stage) throws Exception にTimelineを記述してみたりしましたがやり方がわかりません。public void initialize(URL url, ResourceBundle rb)が怪しそうだなとは考え、また色々したのですが、このタイミングで思い切って質問させていただくことにしました。
どのようにすれば起動時に自動的に作動するようになるのでしょうか?
どうぞよろしくお願いします。
###補足情報(言語/FW/ツール等のバージョンなど)
NetBeans IDE8.2を使用
FXMLはScene Builder 2で作成
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/08 11:23