Java FX中的计时器 [英] Timer in Java FX

查看:125
本文介绍了Java FX中的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JavaFX的Timer中表现出色.
导入了java.util
的计时器 计时器代码:

I have exeption in Timer whith use JavaFX.
timer imported of java.util
code of timer:

  @FXML private Label q1lt;    
  private final Timer t1=new Timer();
  TimerTask t2=new TimerTask(){
         int i = 300000;
         public void run() { 
             if(i==0){
                    file(prim+" "+b+"Балів","D://Результат.txt");
                    t1.cancel();
                    return;
                    }
                    i--;
                    int h,m,s;
                    h=i/360000;
                    m=i/6000%6000;
                    s=i%6000;   
                    q1lt.setText("Залишилось часу "+h+":"+m+":"+s/100);
                    }

                };
        t1.schedule(t2,10,10);   

无效文件:

public void file(String text,String fileName) {
     try {
            File file = new File(fileName);
            if(!file.exists()){
            file.createNewFile();
                 }
            PrintWriter out = new PrintWriter(file.getAbsoluteFile());
             try {
                     out.print(text);
                 } finally {
                     out.close();
                 }
             } catch(IOException exe) {
                 throw new RuntimeException(exe);
             }

证明代码:

 Exception in thread "Timer-1" java.lang.IllegalStateException: Not on FX     application thread; currentThread = Timer-1
          at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
      at    com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.jav  a:423)
    at javafx.scene.Parent$2.onProposedChange(Parent.java:367)
    at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:113)
      at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:108)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:575)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(LabeledSkinBase.java:204)
    at com.sun.javafx.scene.control.skin.LabelSkin.handleControlPropertyChanged(LabelSkin.java:49)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
    at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
    at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:103)
    at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:110)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:144)
      at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:49)
    at     javafx.beans.property.StringProperty.setValue(StringProperty.java:65)
    at javafx.scene.control.Labeled.setText(Labeled.java:145)
    at ua.NazarRepyanskiy.Main$1.run(Main.java:313)
    at java.util.TimerThread.mainLoop(Unknown Source)
      at java.util.TimerThread.run(Unknown Source)

导入的转存:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.;

我无法使用平台和时间表.
因此,请重写我的代码. 嗨

I can not use the platform and timeline.
So rewrite my code please. H e l p

推荐答案

根据您得到的异常,您从错误的线程中调用了q1lt.setText()方法.需要从JavaFX应用程序线程调用此方法:

According to the exception you get, you call the q1lt.setText() method from the wrong thread. This method needs to be called from the JavaFX application thread:

    TimerTask t2=new TimerTask(){
    int i = 300000;
    public void run() {
        if(i==0){
            file(prim+" "+b+"Балів","D://Результат.txt");
            t1.cancel();
            return;
        }
        i--;
        int h,m,s;
        h=i/360000;
        m=i/6000%6000;
        s=i%6000;   
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                q1lt.setText("Залишилось часу "+h+":"+m+":"+s/100);
            }
        });
    }
};

这篇关于Java FX中的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆