我的程序基本上是一个安装程序,它需要将文件从jar内部复制到外部而不会干扰GUI。所以我在任务中运行了所有内容,但任务不会运行多次。
这是按钮(FXML):
<Button fx:id="button1" onAction="#startInstall">
这是在控制器中:
@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次它没有做任何事情。我不知道发生了什么。
答案 0 :(得分:2)
根据API文档:
Task类定义了一个无法重用的一次性对象。如果需要可重用的Worker对象,请使用Service类。 Concurrency
每次需要新实例时运行任务:
onAuthStateChanged
或创建服务并启动它......