【问题标题】:Timer in Java FX [duplicate]Java FX中的计时器[重复]
【发布时间】:2017-06-06 08:29:42
【问题描述】:

我在使用 JavaFX 的 Timer 中有例外。
java.util中导入的定时器
定时器代码:

  @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.;

我无法使用平台和时间线。
所以请重写我的代码。 帮助

【问题讨论】:

    标签: java javafx timer javafx-8


    【解决方案1】:

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

        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);
                }
            });
        }
    };
    

    【讨论】:

    • 非常感谢,我知道怎么用 Platform.runLater(new Runnable() {... 但是无法修改我的代码
    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 2015-11-18
    • 2010-10-14
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多