javafx:javafx.scene.layout.Ancorpane无法强制转换为javafx.scene.layout.BorderPane
嘿,伙计们,我对javafx很熟悉,我正在尝试将一个BorderPane转换为一个AnchornPane,同时发生了一个错误,我不知道该怎么办,我正在遵循一个教程,所以请提供帮助javafx:javafx.scene.layout.Ancorpane无法强制转换为javafx.scene.layout.BorderPane,java,android,javafx,javafx-2,Java,Android,Javafx,Javafx 2,嘿,伙计们,我对javafx很熟悉,我正在尝试将一个BorderPane转换为一个AnchornPane,同时发生了一个错误,我不知道该怎么办,我正在遵循一个教程,所以请提供帮助 import java.io.IOException; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.AnchorPa
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
那么如果我不能施放,我该怎么办呢?
BorderPane
扩展Pane
AnchorPane
extendsPane
它们扩展了相同的类,但属于不同的类,并生成不同的对象,这些对象不能相互转换
实例
Lion
扩展BigCat
Tiger
扩展BigCat
你会怎样把一头
狮子变成一头老虎?我也在学习同样的教程,遇到了同样的问题
在我的例子中,在创建RootLayout FXML时,我错误地选择了AnchorPane作为根元素,而不是BorderPane。因此,在initRootLayout()方法中,它尝试将Ancorpane对象强制转换为BorderPane对象。正如在前面的评论中提到的,AnchorPane和BorderPane扩展了Pane,不可能将它们相互转换
因此,如果选择BorderPane作为RootLayout.fxml的根元素,它将正确地将对象强制转换为BorderPane。因此,现在我想知道,是否需要这种铸造
我希望你的问题现在已经解决了。这可能对遇到此问题的其他人有所帮助 添加为新帖子,因为我无法对已接受的答案发表评论,因为缺少分数
根元素是文档层次结构中最低的元素
创建新的空FXML文件时,请在所选IDE中打开该文件进行编辑。请注意,该文件包含类型为
的根标记。这是因为FXML的根容器是AnchorPane类型
如果在SceneBuilder中打开FXML文件并查看文档层次结构的左下方,您将看到AnchorPane作为根元素列出。单击此元素并删除它,然后保存文件。如果您再次在IDE中查看保存的文件,您应该会看到一个完全空白的文件。这是因为您已经完全删除了根元素
现在返回到SceneBuilder中的文件。在库的左上面板中找到BorderPane元素,并将其拖到场景(中间的Scenebuilder面板)上。现在,当您查看文档层次结构时,您将看到BorderPane列为文档的根元素。在Scenebuilder中保存文件并返回IDE。您将看到您的元素现在有一个类型为
的根标记
希望这有助于其他人 是否可以先向上转换到窗格,然后向下转换?不可以,因为堆上的对象保持不变。为什么不将rootLayout
设置为Pane
,或者更好地设置为Parent
,而不是BorderPane
?@James_DParent
或Pane
不支持setCenter(节点n)
method谢谢这对我有帮助。我遵循相同的教程,犯了与您完全相同的错误。我想这应该是答案@elmiomar,在您的代码中,根元素已经被确定为BorderPane
。你如何解决这个问题??