2017-10-21 161 views
0

我正面臨以下問題:我的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> 

什麼我做錯了任何想法?

+0

我在任務啓動後看到一個'progressWater.progressProperty()。unbind();'。雖然沒有跟蹤整個代碼的邏輯。太多了。就像下面的答案一樣,將進度綁定到動畫的值是我以前從未見過任何人做的事情。 –

+0

但是,這是我的邏輯,如果它不能完成,你能幫我解決這個問題,因爲我是一個javafx的完全新手。謝謝! – bgilca

+0

[JavaFX併發性](https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm)可能會解釋它如何工作。 –

回答

0

它立刻,你正在運行的旋轉和更新進度線程之前發生,我..

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

你的進度屬性綁定到別的東西完全是你沒有更新的貌似,你有沒有嘗試刪除此綁定,以允許屬性自己浮動,而不是綁定到從fadein屬性傳播事件?

+0

試過了,但沒有運氣,它仍然沒有更新。 – bgilca