【问题标题】:How can I show a loading gui in javafx如何在 javafx 中显示加载 gui
【发布时间】:2015-10-28 14:40:05
【问题描述】:

此函数加载我的“请稍候...” gui:

public void showLoading(Stage ownerStage){
try {
        FXMLLoader loader = new FXMLLoader();loader.setLocation(MainApp.class.getResource("/de/ern/loading/Loading.fxml"));
        AnchorPane page = (AnchorPane) loader.load();

        LoadingController controller = loader.getController();
        controller.setLoadingText("Please wait...");
        waitStage = new Stage();
        waitStage.setTitle("Please wait");
        waitStage.getIcons().add(new Image(getClass().getResourceAsStream("logo.png")));//setzt das Icon
        waitStage.initModality(Modality.WINDOW_MODAL);
        waitStage.initOwner(ownerStage);
        Scene scene = new Scene(page);
        waitStage.setScene(scene);
        waitStage.show();   
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我该如何调用它,因为当我直接调用它时,它总是空的。仅显示带有标题的窗格。我发现很多代码 sn-ps für Swing 但不是用于 javafx。

编辑:

我从主 gui 的控制器中调用它,如下所示:

mainApp.showLoading(primaryStage);
AllgemeinTab liveWerte = new AllgemeinTab(this);
liveWerte.setName(getAllgTabName());
liveWerte.setBeschreibungDe(getAllgTabBeschreibungDe());

然后,接下来是一个大型数据库事务。

这是我的 FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="64.0" prefWidth="292.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.ernst.loading.LoadingController">
<children>
<ImageView fx:id="imgLoading" fitHeight="25.0" fitWidth="25.0" layoutX="20.0" layoutY="20.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="19.0" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="20.0">
     <image>
        <Image url="@loading.gif" />
     </image>
  </ImageView>
  <Text layoutX="54.0" layoutY="25.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Status:" AnchorPane.topAnchor="17.0">
     <font>
        <Font name="System Bold" size="12.0" />
     </font></Text>
  <Text fx:id="txtLoadingDescription" layoutX="54.0" layoutY="24.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Beschreibung..." wrappingWidth="230.44140625" AnchorPane.topAnchor="30.0" />
</children>
</AnchorPane>

【问题讨论】:

标签: user-interface javafx


【解决方案1】:

您可能想查看Preloader (Oracle Doc)。预加载器是一个非常小的应用程序,它在加载应用程序之前加载阶段,您可以向预加载器发送一些信息以显示进度。

这可能会给你更多选择

【讨论】:

    【解决方案2】:

    您可以通过显示 ProgressIndicator 来显示“加载 GUI”

    所以显示 ProgressIndicator,然后你需要在一个新线程中做你的背景工作,然后你用新的 Content 替换你的 ProgressIndicator。

    final ProgressIndicator progress = new ProgressIndicator();
    progress.setMaxSize(50, 50);
    rootPane.setCenter(progress);
    
    new Thread(new Runnable() {
    
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            Platform.runLater(new Runnable() {
    
                @Override
                public void run() { 
                       rootPane.setCenter(new Label("done"));
                }
            });  
        }
    }).start();
    

    【讨论】:

      猜你喜欢
      • 2016-03-02
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      相关资源
      最近更新 更多