运行一个简单的任务示例

时间:2015-01-31 14:05:09

标签: java javafx

您好我整晚都在尝试这个例子并且没有运气,我找不到解决方案。我有两个文件。

首先是Worker.java,这里是它的内容

import javafx.application.Application;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author brett
 */

public class Worker {

    /**
     * @param args the command line arguments
     * @throws java.lang.Exception
     */


    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        doit();
    }

    private static void doit(){

        try {
            IteratingTask mytask = new IteratingTask(800000);
            mytask.call();
            System.out.println(mytask.getValue());
             int pro = (int) mytask.getProgress();
        System.out.println(pro);
        } catch (Exception ex) {
            Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

接下来是IteratingTask.java文件及其内容

//import javafx.concurrent.Task;
import javafx.application.Application;
import javafx.concurrent.Task;
/**
 *
 * @author brett
 */
public class IteratingTask extends Task<Integer> {
         private final int totalIterations;

         public IteratingTask(int totalIterations) {
             this.totalIterations = totalIterations;
         }

         @Override protected Integer call() throws Exception {
             int iterations;
            // iterations = 0;
             for (iterations = 0; iterations < totalIterations; iterations++) {
                 if (isCancelled()) {
                     updateMessage("Cancelled");
                     break;
                 }
                 updateMessage("Iteration " + iterations);
                 updateProgress(iterations, totalIterations);
             }
             return iterations;
         }
     }

我知道我做错了但是......我只是看不到它。 这是它得到的错误

run:
Jan 31, 2015 11:56:38 PM Worker doit
SEVERE: null
java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:270)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:265)
    at javafx.application.Platform.runLater(Platform.java:81)
    at javafx.concurrent.Task.runLater(Task.java:1211)
    at javafx.concurrent.Task.updateMessage(Task.java:1129)
    at IteratingTask.call(IteratingTask.java:24)
    at Worker.doit(Worker.java:38)
    at Worker.main(Worker.java:31)

BUILD SUCCESSFUL (total time: 0 seconds)

它构建正常....任何建议都会很棒。

1 个答案:

答案 0 :(得分:1)

问题是FX Toolkit,特别是FX Application Thread尚未启动。 update...(...)中的Task方法更新FX应用程序线程上的各种状态,因此对这些方法的调用会导致IllegalStateException,因为没有运行此类线程。

如果您将此代码嵌入到实际的FX应用程序中,它将正常运行。调用launch()会导致FX工具包启动。

另请注意,虽然这将运行,但Task通常打算在后台线程中运行,如下所示:

import javafx.application.Application;
import javafx.scene.Scene ;
import javafx.scene.layout.StackPane ;
import javafx.scene.control.Label ;
import javafx.stage.Stage ;

import java.util.logging.Level;
import java.util.logging.Logger;


public class Worker extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        StackPane root = new StackPane(new Label("Hello World"));
        Scene scene = new Scene(root, 350, 75);
        primaryStage.setScene(scene);
        primaryStage.show();
        doit();
    }

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

    private void doit(){

        try {
            IteratingTask mytask = new IteratingTask(800000);
            // mytask.call();
            Thread backgroundThread = new Thread(mytask);
            backgroundThread.start(); // will return immediately, task runs in background
            System.out.println(mytask.getValue());
             int pro = (int) mytask.getProgress();
        System.out.println(pro);
        } catch (Exception ex) {
            Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}