1
我的程序需要一段时间才能加载,所以在加载时显示加载窗口。然而,当我使用.show()时,我放入的加载指示器不旋转,但由于某种原因,在使用.showAndWait()时会起作用。 我似乎无法弄清楚问题所在。这是调用加载窗口的控制器。JavaFX加载指示器显示时未显示动画
package FX;
import Utility.*;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
/**
* Created by Alex on 5/31/2017.
*/
public class WindowController {
@FXML private TextField startBox;
@FXML private Button genButton;
@FXML private ComboBox startPage;
@FXML private ComboBox endPage;
@FXML private GridPane grid;
private Stage loadingStage;
private Scene loadingScene;
private Graph graph;
@FXML
public void initialize()throws IOException{
AnchorPane pane = FXMLLoader.load(getClass().getResource("LoadingScreen.fxml"));
loadingScene = new Scene(pane);
loadingStage = new Stage();
loadingStage.setTitle("Loading");
loadingStage.setResizable(false);
loadingStage.setScene(loadingScene);
//loadingStage.initStyle(StageStyle.UTILITY);
}
@FXML
public void GeneratePressed() throws IOException{
loadingStage.show();
String page = startBox.getText();
graph = new Graph(page);
ObservableList<String> list = graph.getNamesList();
startPage.setItems(list);
endPage.setItems(list);
loadingStage.close();
}
@FXML void FindPathPressed(){
}
public void dislayLoading(){
}
}
该窗口在GeneratePressed()中调用,并在初始化程序中创建。 另外,我试图让窗口导航栏为不使用
loadingStage.initStyle(StageStyle.UTILITY);
apear但似乎程序崩溃。
Finaly,这里是我的FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="180.0" maxWidth="250.0" minHeight="180.0"
minWidth="250.0" prefHeight="180.0" prefWidth="250.0"
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="FX.LoadingScreenController">
<children>
<Text layoutX="87.0" layoutY="52.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="Loading" textAlignment="CENTER">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Text>
<ProgressIndicator fx:id="load" layoutX="87.0" layoutY="74.0"
prefHeight="72.0" prefWidth="76.0" />
</children>
</AnchorPane>
'GeneratePressed'大概是一个事件处理函数方法,所以它在FX应用程序线程上被调用。所以在FX应用程序线程可以做任何其他事情之前它会运行完成。因此,要么'loadingStage.show()'和'loadingStage.close()'之间没有足够的时间来实际查看窗口,要么阻止FX应用程序线程并阻止它呈现UI,这意味着您可以看不到窗户。如果你有长时间运行的代码,你需要在后台线程上运行它。请参阅[API的文档](http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html)。 –