2016-05-11 4 views
0

イムを更新することはできません私のTimerTaskを機能タイマーを必要とするが、何らかの理由で、私はTimerTaskを私のLabel.setTextの値を更新するために得るカント小さなプログラムを書くlabel.setText

public class Main extends Application { 


Label TimerLabel; 
/* Create a Button named button */ 
Button button; 
/* Create three Radiobuttons named Radio1,Radio2,Radio3 */ 
RadioButton Radio1, Radio2, Radio3; 

Timer QuestionTimer = new Timer(); 
TimerTask QuestionTick = new TimerTask() { 
    @Override 
    public void run() { 
     TimerLabel.setText(String.valueOf(Integer.valueOf(TimerLabel.getText())+1)); 
    } 
}; 

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

/*UI Part*/ 
@Override 
public void start(Stage primaryStage) throws Exception { 


    /*Window(Stage) Title is set to "Try 1"*/ 
    primaryStage.setTitle("Try 1"); 

    /*Button properties*/ 
    button = new Button(); 
    button.setText("Click Me"); 


    //Radio Button 
    Radio1 = new RadioButton(); 
    Radio1.setText("Click Me 1"); 
    Radio1.setOnAction(e->{ 
     System.out.println("Radio Button Clicked"); 
    }); 

    Radio2 = new RadioButton(); 
    Radio2.setText("Click Me 2"); 
    Radio2.setOnAction(e->{ 
     System.out.println("Radio Button 2 Clicked"); 
    }); 

    Radio3 = new RadioButton(); 
    Radio3.setText("Click Me 3"); 
    Radio3.setOnAction(e->{ 
     System.out.println("Radio Button 3 Clicked"); 
    }); 

    TimerLabel = new Label(); 
    TimerLabel.setText("0"); 


    /*Here my layout is defined */ 
    Pane Buttonlayout = new Pane(); 
    button.setLayoutX(200); 
    button.setLayoutY(200); 
    Radio1.setLayoutX(15); 
    Radio1.setLayoutY(20); 
    Radio2.setLayoutX(15); 
    Radio2.setLayoutY(40); 
    Radio3.setLayoutX(15); 
    Radio3.setLayoutY(60); 
    TimerLabel.setLayoutX(100); 
    TimerLabel.setLayoutY(20); 
    Buttonlayout.getChildren().add(button); 
    Buttonlayout.getChildren().add(Radio1); 
    Buttonlayout.getChildren().add(Radio2); 
    Buttonlayout.getChildren().add(Radio3); 
    Buttonlayout.getChildren().add(TimerLabel); 

    /*Here we define the scene (aka everything inside the stage (inside the window))*/ 
    Scene scene1 = new Scene(Buttonlayout,300,250); 


    primaryStage.setScene(scene1); 
    primaryStage.show(); 

    QuestionTimer.scheduleAtFixedRate(QuestionTick,1000,1000); 
} 

} 

だから私のコードのthats 、私はそれのほとんどが愚かだと知っているが、私は最初にタイマーでプログラミングを開始したいとうまくそれは仕事をしなかった。どんな種類の助けをいただければ幸いですか

+0

は、[この](http://stackoverflow.com/questions/11224886/how-do-i-let-multiple-threads-paint-onto-an-awt-component)質問を見てくださいタイマーは別のスレッドで実行されているとみなされる必要があります。 – Aconcagua

答えて

1

java.util.Timerクラスを利用しているようです。ドキュメンテーションの状態Implementation note: All constructors start a timer thread. JavaFX UIは、タイマーでやっている別のスレッドから更新しないでください。

UIを直接更新するのではなく、Platform.runLater(Runnable)を使用して、メインのJavaFXスレッドでUIタスクをスケジュールします。

javafx.application.Platform.runLater(new Runnable() { 
    @Override 
    public void run() { 
    TimerLabel.setText(String.valueOf(Integer.valueOf(TimerLabel.getText())+1)); 
    } 
} 

そして、自分の好きにして、メソッドチェインを取り除いてください。 Integer.valueOfExceptionを投げると、デバッグが容易になります。

String timer_label = TimerLabel.getText(); 
Integer timer_int = Integer.valueOf(timer_label); 
String timer_text = String.valueOf(timer_int + 1); 
TimerLabel.setText(timer_text); 
+0

これはうまくいきました。はい、Integer例外については考えていませんでしたので、私はそのコードも少し微調整しました。 –

関連する問題