java - 如何使用 FXMLLoader.load() - JavaFX 2

标签 java javafx-2 fxml scenebuilder

我正在构建一个 JavaFX使用 JavaFX Scene Builder 的应用程序.该界面是在 Scene Builder 和 FXML 中创建的文件 (main.fxml) 已创建。

要在我的应用程序中使用该接口(interface),我必须使用 FXMLLoader 加载 FXML 文件,但是有一个问题,因为 load() 方法返回一个对象,要构建一个场景,我需要一个父类的实例。

下面是我的 MainClass 的一部分。编译器给出错误,因为页面不是父类型:

Object page = FXMLLoader.load(MainWindowController.class.getResource("main.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.show();

我该怎么做才能正确编译和运行?

这是 FXML 文件 (main.fxml):

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <VBox prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <children>
        <MenuBar minHeight="21.0" prefHeight="21.0" prefWidth="600.0">
          <menus>
            <Menu mnemonicParsing="false" text="File">
              <items>
                <MenuItem mnemonicParsing="false" text="Close" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Edit">
              <items>
                <MenuItem mnemonicParsing="false" text="Delete" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Help">
              <items>
                <MenuItem mnemonicParsing="false" text="About" />
              </items>
            </Menu>
          </menus>
        </MenuBar>
        <ToolBar minHeight="24.0">
          <items>
            <Button fx:id="btFormat" mnemonicParsing="false" text="FORMAT" />
            <Button fx:id="btUnformat" mnemonicParsing="false" text="Unformat" />
            <Button fx:id="btAutoIndent" mnemonicParsing="false" text="Auto-indent" />
            <CheckBox fx:id="cbShowLineNumber" mnemonicParsing="false" text="Show line number" />
          </items>
        </ToolBar>
        <SplitPane dividerPositions="0.5" focusTraversable="true" orientation="VERTICAL" prefHeight="348.0" prefWidth="600.0">
          <items>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
              <children>
                <VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                  <children>
                    <Label text="INPUT - Paste your code here" />
                    <TextArea fx:id="taInput" prefWidth="200.0" wrapText="true" />
                  </children>
                </VBox>
              </children>
            </AnchorPane>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
              <children>
                <VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                  <children>
                    <Label text="OUTPUT - The formatted code" />
                    <TextArea fx:id="taOutput" prefWidth="200.0" wrapText="true" />
                  </children>
                </VBox>
              </children>
            </AnchorPane>
          </items>
        </SplitPane>
      </children>
    </VBox>
  </children>
</AnchorPane>

最佳答案

由于有多种实体可以通过 FXML 加载,您需要自己声明预期的类型。

Parent page = FXMLLoader.<Parent>load(MainWindowController.class.getResource("main.fxml").toExternalForm());

关于java - 如何使用 FXMLLoader.load() - JavaFX 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13754214/

相关文章:

java - 如何在桌面应用程序中使用 CSS 样式使 Javafx 中的文本或图像闪烁?

java - Java中如何通过Document接口(interface)访问外部样式表?

JavaFX SegmentedButton - 全宽按钮

css - 在 JavaFX CSS 中设置 BackgroundImage 路径的正确方法

java - 将 "www."添加到 URL 字符串

java - 如何在 Java Junit TestCase 中设置 session 变量

java - Java 正则表达式中的 Pattern.UNIX_LINES

java - 是什么让 JavaFx 2 任务成功/失败

java - 如何向 vBox 添加标签?

java - C++ 客户端连接到非阻塞 Java NIO 服务器