进度条在javaFX中未更新
问题描述:
我正面临以下问题:我的progressBar未在JavaFx中更新。进度条在javaFX中未更新
我写了下面的代码:
public class FXMLDocumentController implements Initializable {
static final int max = 1000000;
Task task = new Task<Void>() {
@Override public Void call() {
for (int i = 1; i <= max; i++) {
updateProgress(i, max);
}
return null;
}
};
@FXML
public AnchorPane root;
@FXML
public ProgressBar progressWater;
@FXML
public ProgressBar levelGas;
@Override
public void initialize(URL url, ResourceBundle rb) {
if (!Main.isSplashLoaded) {
loadSplashScreen();
}
}
private void loadSplashScreen() {
try {
Main.isSplashLoaded = true;
StackPane pane = FXMLLoader.load(getClass().getResource(("SplashFXML.fxml")));
root.getChildren().setAll(pane);
FadeTransition fadeIn = new FadeTransition(Duration.seconds(3), pane);
fadeIn.setFromValue(0);
fadeIn.setToValue(1);
fadeIn.setCycleCount(1);
FadeTransition fadeOut = new FadeTransition(Duration.seconds(3), pane);
fadeOut.setFromValue(1);
fadeOut.setToValue(0);
fadeOut.setCycleCount(1);
fadeIn.play();
fadeIn.setOnFinished((e) -> {
fadeOut.play();
});
fadeOut.setOnFinished((e) -> {
AnchorPane paneMain;
try {
paneMain = FXMLLoader.load(getClass().getResource("/sample/FXMLDocument.fxml"));
root.getChildren().setAll(paneMain);
paneMain.setOpacity(0);
FadeTransition fadeInmain = new FadeTransition(Duration.seconds(3), paneMain);
fadeInmain.setFromValue(0);
fadeInmain.setToValue(1);
fadeInmain.play();
fadeInmain.setOnFinished((ex)-> {
// progressWater.setProgress(0.5);
double maxlev = progressWater.getPrefWidth();
progressWater.progressProperty().bind(fadeInmain.fromValueProperty());
/**
* Action goes here
*/
new Thread(){
public void run() {
for (double i = 0.0; i < maxlev; i++){
final double step = i;
Platform.runLater(() ->
progressWater.setProgress(step/10));
System.out.printf("Complete: %02.2f%n", step /10);
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}.start();
progressWater.progressProperty().unbind();
/**
* Action ends here
*/
});
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
});
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
当执行代码时,线程的更新,并在控制台打印完成,但是,progressWater栏不更新UI。 下面是我FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.effect.GaussianBlur?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="MainPane" fx:id="root" prefHeight="322.0" prefWidth="232.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.FXMLDocumentController">
<children>
<ImageView fitHeight="322.0" fitWidth="232.0" pickOnBounds="true">
<image>
<Image url="@background.jpg" />
</image>
</ImageView>
<ProgressBar fx:id="progressWater" layoutX="-80.0" layoutY="170.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="5.0" prefWidth="200.0" progress="1.0" rotate="90.0">
<effect>
<GaussianBlur radius="2.0" />
</effect>
</ProgressBar>
<ProgressBar fx:id="levelGas" layoutX="113.0" layoutY="170.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="5.0" prefWidth="200.0" progress="0.0" rotate="90.0">
<effect>
<GaussianBlur radius="2.0" />
</effect>
</ProgressBar>
</children>
</AnchorPane>
什么我做错了任何想法?
答
它立刻,你正在运行的旋转和更新进度线程之前发生,我..
progressWater.progressProperty().bind(fadeInmain.fromValueProperty());
你的进度属性绑定到别的东西完全是你没有更新的貌似,你有没有尝试删除此绑定,以允许属性自己浮动,而不是绑定到从fadein属性传播事件?
+0
试过了,但没有运气,它仍然没有更新。 – bgilca
我在任务启动后看到一个'progressWater.progressProperty()。unbind();'。虽然没有跟踪整个代码的逻辑。太多了。就像下面的答案一样,将进度绑定到动画的值是我以前从未见过任何人做的事情。 –
但是,这是我的逻辑,如果它不能完成,你能帮我解决这个问题,因为我是一个javafx的完全新手。谢谢! – bgilca
[JavaFX并发性](https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm)可能会解释它如何工作。 –