【问题标题】:JavaFX: Empty window is displayed on first attemptJavaFX:第一次尝试时显示空窗口
【发布时间】:2015-10-13 13:48:15
【问题描述】:

前言:Javafxml 的新手 我的应用程序运行良好。我只有一个问题,我知道应该有一个非常简单的解决方案,但我想不出更多。

当我运行我的应用程序时,我得到一个空窗口。 Empty Window

我必须调整窗口大小才能获得所需

Resized Window

@FXML
void invoice(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("/view/Invoice2.fxml"));
    Parent planner;
    try {
        planner = (Parent) loader.load();
        stage.setTitle("Order Details");
        stage.getScene().setRoot(planner);

    } catch (IOException e) {
        e.printStackTrace();
    }

}

FXML 代码:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>

<TitledPane collapsible="false" maxHeight="-Infinity" maxWidth="-Infinity"        minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1200.0" text="Invoice" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.InvoiceController">
  <content>
     <AnchorPane prefHeight="200.0" prefWidth="200.0">
        <children>
          <SplitPane dividerPositions="0.08355795148247978" layoutX="166.0" layoutY="108.0" orientation="VERTICAL" prefHeight="374.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
          <items>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" styleClass="body-default" stylesheets="@main.css" SplitPane.resizableWithParent="false">
                 <children>
                    <Button fx:id="back" layoutX="20.0" layoutY="14.0" mnemonicParsing="false" onAction="#BackButton" prefHeight="37.0" prefWidth="79.0" styleClass="btn-default" stylesheets="@main.css" text="Back" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="14.0" />
                 </children>
              </AnchorPane>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
                 <children>
                    <SplitPane dividerPositions="0.7078260869565217" layoutX="158.0" layoutY="29.0" prefHeight="283.0" prefWidth="577.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                      <items>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                             <children>
                                <TableView fx:id="invoicelisttb1" layoutX="71.0" layoutY="14.0" prefHeight="281.0" prefWidth="404.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                  <columns>
                                    <TableColumn fx:id="order" prefWidth="148.0" text="Order No." />
                                    <TableColumn fx:id="name" prefWidth="167.0" text="Name" />
                                      <TableColumn fx:id="price" prefWidth="173.0" text="Price" />
                                  </columns>
                                </TableView>
                             </children>
                          </AnchorPane>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
                             <children>
                                <Label layoutX="14.0" layoutY="44.0" prefHeight="31.0" prefWidth="91.0" styleClass="bold" stylesheets="@main.css" text="Order No:" AnchorPane.rightAnchor="233.0" />
                                <TextField fx:id="txtorder" editable="false" layoutX="197.0" layoutY="44.0" prefHeight="25.0" prefWidth="83.0" AnchorPane.rightAnchor="58.0" />
                                <Label layoutX="14.0" layoutY="91.0" prefHeight="31.0" prefWidth="77.0" styleClass="bold" stylesheets="@main.css" text="Price:" AnchorPane.rightAnchor="246.0" />
                                <TextField fx:id="txtprice" layoutX="197.0" layoutY="91.0" prefHeight="25.0" prefWidth="83.0" AnchorPane.rightAnchor="58.0" />
                                <Button fx:id="btnprice" layoutX="196.0" layoutY="160.0" mnemonicParsing="false" onAction="#enterPrice" prefHeight="31.0" prefWidth="113.0" styleClass="btn-default" stylesheets="@main.css" text="Enter Price" AnchorPane.rightAnchor="58.0" />
                             </children>
                          </AnchorPane>
                      </items>
                    </SplitPane>
                 </children>
              </AnchorPane>
          </items>
        </SplitPane>
     </children>
  </AnchorPane>
  </content>
  </TitledPane>

【问题讨论】:

  • 请看我编辑的问题
  • 您的主类是否也位于view 文件夹中?
  • 您发布的代码没有任何问题:原因可能在您代码的其他地方。除非您包含minimal reproducible example,否则任何人都无法提供帮助。
  • 不,它位于不同的包中@Maslor
  • 查看我的编辑版本,这次我也包含了 fxml 文件@James_D

标签: javafx fxml fxmlloader


【解决方案1】:

经过几天的努力,我终于通过简单地将整个fxml的结构放在另一个Anchor Pane中来解决它。我不知道为什么会解决它或如何解决它,但是当我将整个 GUI 包装在 Anchor Pane 中时,它开始正常工作。

【讨论】:

  • 听起来很简单,我遇到了同样的问题,这为我解决了。投赞成票,亲爱的。
【解决方案2】:

尝试以下方法:

try {
   Parent planner = FXMLLoader.load(getClass().getResource("/view/Invoice2.fxml"));

   stage.setTitle("Order Details");
   stage.setScene(new Scene(planner));
} catch (IOException e) {
   e.printStackTrace();
}

我使用的是stage.setScene(new Scene(planner));,而不是stage.getScene().setRoot(planner)

【讨论】:

  • 感谢您的快速回复。这个我已经试过了。这段代码对所有其他窗口都很好,只是这个特定的窗口有问题
  • 您的窗口的初始大小是多少?你有Pref WidthPref Height 吗?还要检查 IDE 中的 fxml 文件(不在 Scene Builder 中)。有时可能会出现错误。
  • 是的,我已经设置了这两个属性。当我用我的 IDE 打开它时,它工作正常。只是当我运行应用程序时,这个窗口看起来完全是空的,当我调整它的大小时,它工作正常。
  • 嗯,有点奇怪。您可以使用包含一致窗口的程序的最小摘录来编辑您的问题吗?
【解决方案3】:

将资源改为"../view/Invoice2.fxml",而不是loader.setLocation(getClass().getResource("/view/Invoice2.fxml"));。如果您的项目目录结构是这样的,这将起作用:

project
|
\_ view
|  |
|  \_ Invoice2.fxml
\_ main
   \_ Main.java

"../path" 表示所选路径在同一个父目录中,而不是在我们当前目录中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2012-10-17
    相关资源
    最近更新 更多