java - 任务只运行一次

标签 java javafx fxml

我的程序基本上是一个安装程序,它需要将文件从 jar 内部复制到外部而不干扰 GUI。因此,我运行了任务中的所有内容,但该任务不会运行多次。

这是按钮 (FXML):

<Button fx:id="button1" onAction="#startInstall">

这是在 Controller 中:

@FXML
public Button button1;

public void startInstall() {
    if (!working) {
        working = true;
        installoption = true;
        advanced = false;
        System.out.println("pressed install");
        new Thread(normalInstall).start();
        System.out.println("after task call");
        return;
    }
}

这就是任务:

@SuppressWarnings("rawtypes")
Task normalInstall = new Task<Void>() {

    public void normalInstall() {

        setGUI(null,"Getting package info.");
        JSONObject info = getInfo();
        System.out.println(info);
        String mpmUUID = (String) info.get("UUID");
        setGUI(null,"Setting up folders.");
        --SNIP--
        deleteTemp(mpmUUID);
        setGUI(null,"Install has finished.");
        working = false;
        return;
    }


    @Override public Void call() {
        if(installoption == true) {
            if (advanced == true) {
                JFileChooser chooser = new JFileChooser();
                chooser.setCurrentDirectory(new java.io.File("."));
                chooser.setDialogTitle("choose install directory.");
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.setAcceptAllFileFilterUsed(false);

                if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    System.out.println("directory choosen: " + chooser.getSelectedFile().toString());
                    customPathBool = true;
                    customPath = chooser.getSelectedFile().toString();
                } else {
                    System.out.println("No Selection ");
                }
            }
            JSONObject info = getInfo();
            String uuid = (String) info.get("UUID");
            String profile = getProfileList(getProfiles(),uuid);
            Integer installerversion = Integer.parseInt((String)(info.get("mapVersion")));
            Integer installedversion = null;
            if (profile != null) {
                installedversion = getProfileVersion(getProfiles(),profile);
            }
            if (profile == null) {
                normalInstall();
                return null;
            } else {
                int n = JOptionPane.showConfirmDialog(
                        null,
                        "It seems that this map is already installed.\n Do you want to reset the map?",
                        "Map already installed",JOptionPane.WARNING_MESSAGE,
                        JOptionPane.YES_NO_OPTION);
                if (n == JOptionPane.YES_OPTION) {
                    if (installerversion < installedversion) {
                        int b = JOptionPane.showConfirmDialog(
                                null,
                                "You have an outdated version of the installer.\n Do you want to continue?",
                                "Older version",JOptionPane.WARNING_MESSAGE,
                                JOptionPane.YES_NO_OPTION);
                        if (b == JOptionPane.YES_OPTION) {
                            unInstall();
                            normalInstall();
                            return null;
                        } else {
                            setGUI(null,"Install canceled.");
                            return null;
                        }
                    } else {
                        unInstall();
                        normalInstall();
                        return null;
                    }
                } else {
                    setGUI(null,"Install canceled.");
                    return null;
                }
            }
        } else {
            unInstall();
            return null;
        }
    }
};

这是控制台:

    {"MCVersion":"1.11","pathOptions":null,"pathWorld":"world.zip","name":"uninstall test","mapVersion":"1","icon":"TNT","UUID":"5b2e55ed-bb5c-48e0-bbf5-49a7851453bb","pathPack":"pack.zip"}
pressed install
after task call
[a63d52eda9384a7ca1607d8ea0b2fe53, 44d01181eae635d31f2cefe5e1f75cd4, e0e96e422659dfdc1ad16d53a37ee618]
a63d52eda9384a7ca1607d8ea0b2fe53
--SNIP--
finished creating profile
pressed install
after task call

我按了按钮 5 次。它第一次做了它应该做的事情。 第二次,所有内容都在任务之前和之后运行,但没有执行任务本身。 3次它什么也没做。我不知道发生了什么。

最佳答案

根据API文档:

Task 类定义了一个无法重用的一次性对象。如果您需要可重用的 Worker 对象,请使用 Service 类。 Concurrency

要在每次需要它的新实例时运行该任务:

NormalInstallTask normalInstall = new NormalInstallTask ();
new Thread(normalInstall).start();

或者创建一个服务并启动它...

关于java - 任务只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44032252/

相关文章:

java - 不确定使用 BufferedReader 根据 boolean 值检查 .txt 文件中的信息

java - 将按钮禁用属性绑定(bind)到 fxml 中的 TreeView 选择

java - 在谷歌应用程序引擎项目中使用java小程序

java - 线程中的异常 "main"java.lang.StringIndexOutOfBoundsException : String index out of range

java - Spring quartz 的问题

java.sql.SQLSyntaxErrorException : ORA-02289: sequence does not exist while using default Generation Strategy in Hibernate with Oracle

Javafx仿射变换

java - 使用 AWT Robot 在 Mac 上切换应用程序有时有效

Java:应用程序启动方法中出现异常 java.lang.reflect.InitationTargetException

java - 在 GUI 中与 JavaFX 网络服务交互