gpt4 book ai didi

javafx:使用任务更新 UI,然后停止执行

转载 作者:行者123 更新时间:2023-12-02 13:42:35 25 4
gpt4 key购买 nike

我正在开发一个应用程序来检测网络钓鱼网站,使用 javafx。我有一个“预测”按钮,它在其事件处理程序中执行所有必要的处理。我还希望能够更新 ProgressBar,其中包含有关处理过程中进度的信息。我为此使用了一个任务,调用 updateProgress 和 updateValue 来获取最终结果。

但是,如果发生异常,我想更新 UI,然后立即终止 EventHandler 的执行,使用 updateProgress 和一些错误值。但是,updateProgress 不会立即更新 UI。是否存在某种东西不仅可以从 EventHandler 内部更新 UI(例如 Task),而且还可以让我准确控制 UI 更新的时间?

作为引用,这是我完整的事件处理程序代码:

 predict.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event)
{
Task<Result> task = new Task<Result>()
{
@Override
protected Result call() throws Exception
{
String url = urlText.getText();
ArffData arffData = new ArffData();
try
{
updateProgress(1, 10);
URL uri = new URL(url);

String domain = uri.getHost();
arffData.setUrlSimilarity(DataGatherer.readLevenshtein(domain));
updateProgress(2, 10);
boolean redirection = DataGatherer.getRedirectionStatus(url);

arffData.setRedirection(redirection);
updateProgress(3, 10);
Response response = Jsoup.connect(url).execute();
arffData.setSpellingErrors(DataGatherer.getSpellingErrors(response).size());
}
catch (IOException e1)
{
updateProgress(-1, 10); //Should update UI before terminating
return null;
}



Classifier rf;
Instances instances;
try
{
updateProgress(4, 10);
rf = (Classifier) SerializationHelper.read("RF100.model");
instances = new DataSource("phishingData.arff").getDataSet();
}
catch (Exception e1)
{
updateProgress(-1, 10);
return null;
}
if (instances.classIndex() == -1)
instances.setClassIndex(instances.numAttributes() - 1);
String offers = offerText.getValue();
String lf = lfText.getValue();
updateProgress(5, 10);
Instance inst = InstanceSetup.setUpInstance(arffData, offers, lf, instances);

try
{
updateProgress(6, 10);
double clsLabel = rf.classifyInstance(inst);
instances.add(inst);
rf.buildClassifier(instances);
SerializationHelper.write("RF100.model", rf);
Evaluation eval = new Evaluation(instances);
eval.crossValidateModel(rf, instances, 10, new Random(1));
boolean phishing = clsLabel ==0 ?true: false;
Result result = new Result(phishing, eval.pctCorrect());
updateProgress(10, 10);
if(clsLabel == 0)
{
predictionLabel.setText("the given website IS a phishing website.");
}
else
{
predictionLabel.setText("the given website IS NOT a phishing website.");
}
updateValue(result);
accuracyLabel.setText("PhishGuard is " + String.format("%.4f%%", eval.pctCorrect()) +
" confident in this prediction.");
return result;

}
catch (Exception e)
{
updateProgress(-1, 10);
return null;
}

}
};

task.progressProperty().addListener(new ChangeListener<Number>()
{

@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
switch(newValue.intValue())
{
case 1:
{
pBar.setProgress(10);
progressLabel.setText(progressLabels[1]);
}
case 2:
{
pBar.setProgress(30);
progressLabel.setText(progressLabels[2]);
}
case 3:
{
pBar.setProgress(50);
progressLabel.setText(progressLabels[3]);
}
case 4:
{
pBar.setProgress(70);
progressLabel.setText(progressLabels[4]);
}
case 5:
{
pBar.setProgress(80);
progressLabel.setText(progressLabels[5]);
}
case 6:
{
pBar.setProgress(90);
progressLabel.setText(progressLabels[6]);
}
case 10:
{
pBar.setProgress(100);
progressLabel.setText(progressLabels[0]);
}
case -1:
{
predictionLabel.setText("a prediction could not be made.");
accuracyLabel.setText("");
pBar.setProgress(0);
progressLabel.setText(progressLabels[0]);
}

}


}

});

task.valueProperty().addListener(new ChangeListener<Result>(){

@Override
public void changed(ObservableValue<? extends Result> observable, Result oldValue, Result newValue)
{
// TODO Auto-generated method stub
boolean phishing = newValue.isPhishing();
if(phishing)
{
predictionLabel.setText("the given website IS a phishing website.");
}
else
{
predictionLabel.setText("the given website IS NOT a phishing website.");
}

accuracyLabel.setText("PhishGuard is " + String.format("%.4f%%", newValue.getAccuracy()) +
" confident in this prediction.");

}

});
new Thread(task).start();
}});

最佳答案

TaskprogressProperty()ProgressBar要么是不确定,要么设置为 0 到 1(含)之间的值。对于进度条:

A positive value between 0 and 1 indicates the percentage of progress where 0 is 0% and 1 is 100%. Any value greater than 1 is interpreted as 100%.

所以当你观察任务的进度值,然后获取intValue()时,只有三种可能:-1(当进度不确定时)、1(任务完成时)或 0(所有其他值)。当您使用任务的 progressProperty 注册监听器时,它已经处于 INDETERMINATE 状态,因此您看到的第一个更改具有 intValue() 0(与您的任何情况都不匹配),并且所有后续更改都会看到相同的值,直到任务完成(当您看到 1 时)。此时,您将进度条的进度设置为 10,如上所述,它被解释为 100%。

可能您真正想要做的只是将进度条的进度属性绑定(bind)到任务的进度属性(这样它们就会一起增加)。

要定期更新文本,您可以使用任务的 messageProperty

因此,我将完全删除任务进度属性上的监听器,并将其替换为

pBar.progressProperty().bind(task.progressProperty());
pBar.textProperty().bind(task.messageProperty());

根据任务中需要的每条消息调用 updateMessage()(稍后编写代码)。

为此:

However, in the event of an exception, I want to update the UI then immediately terminate execution of the EventHandler

我假设您的真正意思是“终止任务的执行”,因为事件处理程序早已完成。正如注释中所述,Task 默认情况下处理异常。因此,您可以让异常从 call() 方法传播,并在 onFailed 处理程序中执行 UI 更新。

结果看起来像这样:

predict.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event)
{
Task<Result> task = new Task<Result>()
{
@Override
protected Result call() throws Exception
{
String url = urlText.getText();
ArffData arffData = new ArffData();

updateProgress(1, 10);
updateMessage(progressLabels[1]);
URL uri = new URL(url);

String domain = uri.getHost();
arffData.setUrlSimilarity(DataGatherer.readLevenshtein(domain));
updateProgress(2, 10);
updateMessage(progressLabels[2]);

boolean redirection = DataGatherer.getRedirectionStatus(url);

arffData.setRedirection(redirection);
updateProgress(3, 10);
updateMessage(progressLabels[3]);
Response response = Jsoup.connect(url).execute();
arffData.setSpellingErrors(DataGatherer.getSpellingErrors(response).size());






Classifier rf;
Instances instances;

updateProgress(4, 10);
updateMessage(progressLabels[4]);
rf = (Classifier) SerializationHelper.read("RF100.model");
instances = new DataSource("phishingData.arff").getDataSet();
if (instances.classIndex() == -1)
instances.setClassIndex(instances.numAttributes() - 1);
String offers = offerText.getValue();
String lf = lfText.getValue();
updateProgress(5, 10);
updateMessage(progressLabels[5]);

Instance inst = InstanceSetup.setUpInstance(arffData, offers, lf, instances);

updateProgress(6, 10);
updateMessage(progressLabels[6]);

double clsLabel = rf.classifyInstance(inst);
instances.add(inst);
rf.buildClassifier(instances);
SerializationHelper.write("RF100.model", rf);
Evaluation eval = new Evaluation(instances);
eval.crossValidateModel(rf, instances, 10, new Random(1));
boolean phishing = clsLabel ==0 ?true: false;
Result result = new Result(phishing, eval.pctCorrect());
updateProgress(10, 10);
updateMessage(progressLabels[0]);

if(clsLabel == 0)
{
predictionLabel.setText("the given website IS a phishing website.");
}
else
{
predictionLabel.setText("the given website IS NOT a phishing website.");
}
updateValue(result);
accuracyLabel.setText("PhishGuard is " + String.format("%.4f%%", eval.pctCorrect()) +
" confident in this prediction.");
return result;


}
};

pBar.progressProperty().bind(task.progressProperty());
pLabel.textProperty().bind(task.messageProperty());


task.valueProperty().addListener(new ChangeListener<Result>(){

@Override
public void changed(ObservableValue<? extends Result> observable, Result oldValue, Result newValue)
{
// TODO Auto-generated method stub
boolean phishing = newValue.isPhishing();
if(phishing)
{
predictionLabel.setText("the given website IS a phishing website.");
}
else
{
predictionLabel.setText("the given website IS NOT a phishing website.");
}

accuracyLabel.setText("PhishGuard is " + String.format("%.4f%%", newValue.getAccuracy()) +
" confident in this prediction.");

}

});

task.setOnFailed(e -> {
predictionLabel.setText("a prediction could not be made.");
accuracyLabel.setText("");
pBar.progressProperty().unbind();
pBar.setProgress(0);
progressLabel.setText(progressLabels[0]);
});

new Thread(task).start();
}});

关于javafx:使用任务更新 UI,然后停止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42658467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com