如何使用FXMLLoader.load() - JavaFX 2 [英] How to use FXMLLoader.load() - JavaFX 2

查看:368
本文介绍了如何使用FXMLLoader.load() - JavaFX 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JavaFX 应用程序/www.oracle.com/technetwork/java/javafx/tools/index.html\"rel =noreferrer> JavaFX Scene Builder 。界面是在Scene Builder中创建的,并且是 FXML 文件(main.fxml)已创建。

I am building a JavaFX application using the JavaFX Scene Builder. The interface was created in the Scene Builder and a FXML file (main.fxml) was created.

要在我的应用程序中使用该接口,我必须使用 FXMLLoader ,但是有一个问题因为load()方法返回一个Object,并且构建一个场景我需要一个Parent类的实例。

To use the interface in my application I must load the FXML file using the FXMLLoader, but there is a problem because the load() method returns an Object, and to build a Scene I need an instance of Parent class.

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

Below is a piece of my MainClass. The compiler is giving an error because page is not of type Parent:

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

如何使编译和运行正确?

What do I do to make this compile and run correctly?

这是FXML文件(main.fxml):

Here is the FXML file (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你需要自己陈述预期的类型。

Due to a variety of entities eligible to be loaded through FXML you need to state the expected type yourself.

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

这篇关于如何使用FXMLLoader.load() - JavaFX 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆