【发布时间】:2017-06-01 02:19:12
【问题描述】:
我的程序需要一些时间来加载,所以在加载时,我会显示一个加载窗口。但是,当我使用 .show() 时,我放入的加载指示器不会旋转,但由于某种原因在使用 .showAndWait() 时会起作用。 我似乎无法弄清楚问题是什么。这是调用加载窗口的控制器。
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() 中调用,并在初始化程序中创建。 另外,我试图通过使用来让 Windows 导航栏不出现
loadingStage.initStyle(StageStyle.UTILITY);
但这似乎会使程序崩溃。
最后,这是我的 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 docs forTask。
标签: java user-interface javafx