gpt4 book ai didi

java - 如何将新的 FXML 场景加载到 Broder Pane 中

转载 作者:太空宇宙 更新时间:2023-11-04 09:07:51 25 4
gpt4 key购买 nike

private void loadUI(String ui) {
BorderPane root = null;
try {
root = FXMLLoader.load(getClass().getResource(ui + ".fxml"));
} catch (Exception e) {

}
borderPaneCenter.setCenter(root);
}

和包中的 ui.fxml 文件,但仍然不起作用

这是异常(exception)


Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
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:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

ui.fxml 文件在 anchor Pane (具有边框 Pane )中,并且在这个(主场景)场景中,我想在此(主边框 Pane )边框 Pane 的中心设置

最佳答案

您收到消息参数类型不匹配。当您尝试将 FXML 文件加载到不同类型的变量中时,会收到此错误消息。

您的root变量是BorderPane,但您说您的FXML文件的根是AnchorPane。要修复此错误,这些类型必须匹配。

您可以将 root 更改为 AnchorPane,但这依赖于 FXML 文件的根为 AnchorPane。例如,如果您稍后决定将整个场景包装在 ScrollPane 中,您的代码将再次中断。

更好的解决方案是将root更改为Parent。所有容器都扩展 Parent,因此无论根容器是什么容器,您的代码都将正常工作:

Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource(ui + ".fxml"));
}

如果您需要通过代码控制 BorderPane,只需在 FXML 文件中为其指定一个 fx:id,然后在 Controller 中创建一个与 id 同名的 BorderPane 变量。例如:

*.fxml

...
<BorderPane fx:id="borderPane" ... >
...

Controller.java

@FXML
private BorderPane borderPane; // name borderPane must match fx:id above

关于java - 如何将新的 FXML 场景加载到 Broder Pane 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60016105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com