javafx : javafx.scene.layout.AnchorPane 无法转换为 javafx.scene.layout.BorderPane

javafx : javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.BorderPane

大家好,我认识 javafx,我正在尝试将 BorderPane 转换为 anchronPane,同时发生错误,我不知道该怎么做,我正在学习教程,所以请帮忙

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("AddressApp");

        initRootLayout();
        showPersonOverview();
    }

    /**
     * Initializes the root layout.
     */
    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Shows the person overview inside the root layout.
     */
    public void showPersonOverview() {
        try {
            // Load person overview.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
            AnchorPane personOverview = (AnchorPane) loader.load();

            // Set person overview into the center of root layout.
            rootLayout.setCenter(personOverview);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns the main stage.
     * @return
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我收到了这个错误

    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
atcom.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
at com.sun.javafx.application.LauncherImpl.access[=13=]0(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.BorderPane
at ch.makery.address.MainApp.initRootLayout(MainApp.java:35)
at ch.makery.address.MainApp.start(MainApp.java:22)
at com.sun.javafx.application.LauncherImpl.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:298)
atcom.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.ja    va:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access0(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication.run(WinApplication.java:112)
... 1 more

异常运行应用程序ch.makery.address.MainApp Java 结果:1

所以,如果我不能施法,那我该怎么办?

BorderPane 扩展 Pane

AnchorPane 扩展 Pane

它们扩展相同的 class 但不同 classes 并生成不能相互转换的不同对象。


实例

Lion 扩展 BigCat

Tiger 扩展 BigCat

如何将 Lion 转换为 Tiger

我也在学习同样的教程,遇到了同样的问题。

在我的例子中,在创建 RootLayout FXML 时,我错误地 selected AnchorPane 作为根元素而不是 BorderPane.因此,在 initRootLayout() 方法中,它尝试将 AnchorPane 对象转换为 BorderPane 对象。正如之前的评论中提到的,AnchorPane 和 BorderPane 扩展了 Pane,它们不能相互转换!

因此,如果您 select BorderPane 作为 RootLayout.fxml 的根元素,它会正确地将对象转换为 BorderPane。因此,现在我想知道,是否需要这种转换?

我希望你的问题现在已经解决了。这可能对遇到此问题的其他人有所帮助!

添加为新的post,因为我无法对已接受的答案发表评论,因为积分不足。

根元素是文档层次结构中的最低元素。

创建新的空 FXML 文件时,在您选择的 IDE 中打开文件进行编辑。请注意,该文件包含 <AnchorPane> 类型的根标签。这是因为 FXML 的根容器是 AnchorPane 类型。

如果您在 SceneBuilder 中打开 FXML 文件并查看左下角的文档层次结构,您将看到 AnchorPane 被列为您的根元素。单击此元素并将其删除,然后保存您的文件。如果您再次查看 IDE 中保存的文件,您应该会看到一个完全空白的文件。这是因为您已经完全删除了根元素。

现在 return 到您在 SceneBuilder 中的文件。在左上方面板的库中找到 BorderPane 元素,并将其拖到场景中(中间的 Scenebuilder 面板)。现在,当您查看文档层次结构时,您会看到 BorderPane 列为文档的根元素。将您的文件保存在 Scenebuilder 中,并将 return 保存到您的 IDE。您会看到您的元素现在有一个 <BorderPane>.

类型的根标签

希望这对外面的人有帮助!