簡體   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