Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行一个简单的任务示例_Java_Javafx - Fatal编程技术网

Java 运行一个简单的任务示例

Java 运行一个简单的任务示例,java,javafx,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 Propertie

嗨,我一整晚都在试着运行这个例子,但一直没有运气,所以我找不到解决方案。我有两个文件

首先是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);
        }

    }
}
//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;
         }
     }
接下来是IteratingTask.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);
        }

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

它建造得很好。。。。任何建议都会很棒。

问题在于FX工具包,尤其是FX应用程序线程尚未启动。
Task
中的
update…(…)
方法更新FX应用程序线程上的各种状态,因此对这些方法的调用会导致
IllegalStateException
,因为没有这样的线程在运行

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

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

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

    }
}

这和Swing或IDE有什么关系?代码似乎是基于Java FX的(Swing的另一个工具包),我怀疑您的IDE是否会自动修复这个问题(或导致当前代码失败)…对不起,我真的迷路了,我正在努力学习,我的理解仍然非常重要