繁体   English   中英

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$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.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$8.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.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.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
... 1 more

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

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

我也遵循相同的教程,遇到了同样的问题。

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

因此,如果选择BorderPane作为RootLayout.fxml的根元素,它会正确地将对象强制转换为BorderPane。 因此,现在我想知道,如果甚至需要这种铸造?

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

BorderPane扩展了Pane

AnchorPane扩展了Pane

它们扩展了相同的类,但是它们是不同的类,并生成不能相互转换的不同对象。


实际的例子

Lion扩展了BigCat

Tiger扩展了BigCat

你怎么会把Lion投给Tiger

添加为新帖子,因为由于缺乏积分,我无法评论已接受的答案。

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

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

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

现在返回到SceneBuilder中的文件。 在左上方面板的库中找到BorderPane元素并将其拖到场景中(Scenebuilder中间面板)。 现在,当您查看文档层次结构时,您将看到BorderPane被列为文档的根元素。 将文件保存在Scenebuilder中并返回IDE。 您将看到您的元素现在具有<BorderPane>类型的根标记。

希望这有助于那里的人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM