进度条未在javaFX中更新

时间:2017-10-21 16:09:24

标签: javafx

我遇到以下问题:我的progressBar没有在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>

关于我做错了什么的任何想法?

1 个答案:

答案 0 :(得分:0)

在运行旋转并更新进度的线程之前,我立即想到你。

                progressWater.progressProperty().bind(fadeInmain.fromValueProperty());

您将progress属性绑定到其他完全没有更新的东西,您是否尝试删除此绑定以允许该属性自行浮动而不依赖于fadein属性中的传播事件?