简体   繁体   English

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

[英]javafx : javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.BorderPane

hey guys i am knew to javafx and i am trying to cast a BorderPane to an anchronPane, meanwhile an error occured and i don t know what to do, i was following a tutorial so please help 嘿家伙我知道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);
    }
}

and I am getting this error 我收到了这个错误

    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

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

so if i cannot cast then what sould i do? 所以,如果我不能投,那我该怎么办?

I was following the same tutorial as well, and encountered the same issue. 我也遵循相同的教程,遇到了同样的问题。

In my case, while creating the RootLayout FXML, I erroneously selected AnchorPane as the root element instead of BorderPane . 在我的例子中,在创建RootLayout FXML时,我错误地选择了AnchorPane作为根元素而不是BorderPane So, in the initRootLayout() method, it tried to Cast AnchorPane object to BorderPane object. 因此,在initRootLayout()方法中,它尝试将AnchorPane对象转换为BorderPane对象。 And as mentioned in the previous comment that, AnchorPane and BorderPane extend Pane, it is not possible to cast them with each other! 正如前面评论中提到的那样,AnchorPane和BorderPane扩展了Pane,不可能将它们相互投射!

Thus, if you select BorderPane as the root element for RootLayout.fxml, it correctly casts the object to BorderPane. 因此,如果选择BorderPane作为RootLayout.fxml的根元素,它会正确地将对象强制转换为BorderPane。 Thus, now I am wondering, if this casting is even required? 因此,现在我想知道,如果甚至需要这种铸造?

I hope your issue is resolved by now. 我希望你的问题现在得到解决。 This might be helpful to someone else who encounters this problem! 这可能对遇到此问题的其他人有所帮助!

BorderPane extends Pane BorderPane扩展了Pane

AnchorPane extends Pane AnchorPane扩展了Pane

They extend the same class but are different classes and generate different objects that can not be casted into each other. 它们扩展了相同的类,但是它们是不同的类,并生成不能相互转换的不同对象。


Practical example 实际的例子

Lion extends BigCat Lion扩展了BigCat

Tiger extends BigCat Tiger扩展了BigCat

How would you cast a Lion to a Tiger ? 你怎么会把Lion投给Tiger

Adding as a new post since I am unable to comment on the accepted answer due to lack of points. 添加为新帖子,因为由于缺乏积分,我无法评论已接受的答案。

The root element is the lowest element in your document hierarchy. 根元素是文档层次结构中的最低元素。

When you create a new empty FXML file, open the file for editing in your IDE of choice. 创建新的空FXML文件时,请在所选的IDE中打开要编辑的文件。 Note that the file contains root tags of type <AnchorPane> . 请注意,该文件包含<AnchorPane>类型的根标记。 This is because the root container of your FXML is a type AnchorPane. 这是因为FXML的根容器是AnchorPane类型。

If you open the FXML file in SceneBuilder and look in the lower left hand side at the document hierarchy you will see the AnchorPane listed as your root element. 如果在SceneBuilder中打开FXML文件并查看文档层次结构的左下角,则会看到AnchorPane列为根元素。 Click on this element and delete it then save your file. 单击此元素并将其删除,然后保存文件。 If you view your saved file in your IDE again you should see a completely blank file. 如果再次在IDE中查看已保存的文件,则应该看到一个完全空白的文件。 This is because you have removed your root element completely. 这是因为您已完全删除了根元素。

Now return to your file in SceneBuilder. 现在返回到SceneBuilder中的文件。 Find the BorderPane element in the library in the upper left panel and drag it onto your scene (the middle Scenebuilder panel). 在左上方面板的库中找到BorderPane元素并将其拖到场景中(Scenebuilder中间面板)。 Now when you look at the document hierarchy you will see that the BorderPane listed as the root element of your document. 现在,当您查看文档层次结构时,您将看到BorderPane被列为文档的根元素。 Save your file in Scenebuilder and return to your IDE. 将文件保存在Scenebuilder中并返回IDE。 You will see your element now has a root tag of type <BorderPane> . 您将看到您的元素现在具有<BorderPane>类型的根标记。

Hope this helps someone out there! 希望这有助于那里的人!

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

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