####前提・実現したいこと
JavaFX,FXMLアプリケーションでGUIデジタル時計をanimation.Timelineを使って作成したい。
####発生している問題
起動時に時計の自動スタートの為のTimelineの使い方がわからない。
たぶんTimelineかEventHandlerの使い方の間違いを犯しているのだと思うのですが、どうにも情報が少なくわかりません。
すっかり詰んでしまっています。
プロの方から見ればしょうもないエラーなのでしょうが また質問させていただきます。申し訳ないのですがどのように修正すれば良いのでしょうか?
####コンパイル時のエラーメッセージ
\FXMLDocumentController.java:58: エラー: <identifier>がありません
label.setText(txt);
D:\一括\新しいフォルダー\NetBeansProjects\Tokei3\src\tokei3\FXMLDocumentController.java:58: エラー: <identifier>がありません
label.setText(txt);
エラー2個
###該当のソースコード
java
1/* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6package tokei3; 7 8import java.net.URL; 9import java.time.LocalTime; 10import java.util.ResourceBundle; 11import javafx.event.ActionEvent; 12import javafx.fxml.FXML; 13import javafx.fxml.Initializable; 14import javafx.scene.control.Label; 15import javafx.animation.Timeline; 16import javafx.animation.KeyFrame; 17import javafx.util.Duration; 18import javafx.event.*; 19/** 20 * 21 * @author test 22 */ 23public class FXMLDocumentController implements Initializable { 24 25 @FXML 26 Label label; 27 28 @FXML 29 private void handleButtonAction(ActionEvent event) { 30 //何も書いていない。 31 } 32 @Override 33 public void initialize(URL url, ResourceBundle rb) { 34 35 //Timelineインスタンス作成、同時にKeyFrameインスタンスも作成し、1000millisごとのアニメーション動作を指定 36 Timeline timeline = new Timeline(new KeyFrame(Duration.millis(1000)));**(後日メモ!ここに主要な問題がありました!)** 37 38 //EventHandler<actionevent>(){}の{}内に処理したい内容を書く 39 new EventHandler<actionevent>(){ 40 41 //現在時刻を取得 42 LocalTime now=LocalTime.now(); 43 //ラベル表示させる値をLocalTimeから取得 44 String txt=String.format("%02d:%02d:%02d", 45 now.getHour(), 46 now.getMinute(), 47 now.getSecond()); 48 //ラベルに取得した値を表示 49 label.setText(txt); 50 };//ここまでが{}の中に書いた処理したい内容 51 52 //アニメーションの作動期間は無限を指定 53 timeline.setCycleCount(Timeline.INDEFINITE); 54 //アニメーション開始 55 timeline.play(); 56 //動きません、なんでだろう 57 } 58}
java
1/* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6package tokei3; 7 8import javafx.application.Application; 9import javafx.fxml.FXMLLoader; 10import javafx.scene.Parent; 11import javafx.scene.Scene; 12import javafx.stage.Stage; 13 14/** 15 * 16 * @author test 17 */ 18public class Tokei3 extends Application { 19 20 @Override 21 public void start(Stage stage) throws Exception { 22 23 24 Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 25 26 Scene scene = new Scene(root); 27 28 stage.setScene(scene); 29 stage.show(); 30 } 31 32 /** 33 * @param args the command line arguments 34 */ 35 public static void main(String[] args) { 36 launch(args); 37 } 38} 39
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="tokei3.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>
回答1件
あなたの回答
tips
プレビュー