java javafx:javafx.scene.layout.AnchorPane 不能转换为 javafx.scene.layout.BorderPane
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35956527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javafx : javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.BorderPane
提问by Firas Chebbah
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##代码##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
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?
所以如果我不能施展,那我该怎么办?
回答by Tejas Shah
I was following the same tutorial as well, and encountered the same issue.
我也在遵循相同的教程,并遇到了同样的问题。
In my case, while creating the RootLayout FXML, I erroneously selected AnchorPaneas the root element instead of BorderPane. So, in the initRootLayout() method, it tried to Cast AnchorPane object to BorderPane object. And as mentioned in the previous comment that, AnchorPane and BorderPane extend Pane, it is not possible to cast them with each other!
就我而言,在创建 RootLayout FXML 时,我错误地选择AnchorPane而不是BorderPane作为根元素。因此,在 initRootLayout() 方法中,它尝试将 AnchorPane 对象强制转换为 BorderPane 对象。并且正如之前的评论中提到的,AnchorPane 和 BorderPane 扩展了 Pane,这是不可能相互转换的!
Thus, if you select BorderPaneas the root element for RootLayout.fxml, it correctly casts the object to BorderPane. Thus, now I am wondering, if this casting is even required?
因此,如果您选择BorderPane作为 RootLayout.fxml 的根元素,它会正确地将对象转换为 BorderPane。因此,现在我想知道,是否甚至需要这种铸造?
I hope your issue is resolved by now. This might be helpful to someone else who encounters this problem!
我希望你的问题现在已经解决。这可能对遇到此问题的其他人有所帮助!
回答by Yassin Hajaj
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
?
你会如何将 a 转换Lion
为 a Tiger
?
回答by Jeramy D.
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. Note that the file contains root tags of type <AnchorPane>
. This is because the root container of your FXML is a type AnchorPane.
创建新的空 FXML 文件时,请在您选择的 IDE 中打开该文件进行编辑。请注意,该文件包含类型为 的根标记<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. 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. This is because you have removed your root element completely.
如果您在 SceneBuilder 中打开 FXML 文件并查看文档层次结构的左下方,您将看到 AnchorPane 列为您的根元素。单击此元素并将其删除,然后保存您的文件。如果您再次在 IDE 中查看保存的文件,您应该会看到一个完全空白的文件。这是因为您已经完全删除了根元素。
Now return to your file in SceneBuilder. Find the BorderPane element in the library in the upper left panel and drag it onto your scene (the middle Scenebuilder panel). Now when you look at the document hierarchy you will see that the BorderPane listed as the root element of your document. Save your file in Scenebuilder and return to your IDE. You will see your element now has a root tag of type <BorderPane>
.
现在返回到 SceneBuilder 中的文件。在左上面板的库中找到 BorderPane 元素并将其拖到您的场景中(中间的 Scenebuilder 面板)。现在,当您查看文档层次结构时,您将看到 BorderPane 被列为文档的根元素。将您的文件保存在 Scenebuilder 中并返回到您的 IDE。您将看到您的元素现在有一个类型为 的根标记<BorderPane>
。
Hope this helps someone out there!
希望这可以帮助那里的人!