[英]How to load new FXML with another controller class in the same scene
I've tried to load new FXML by 我试图通过加载新的FXML
URL url = this.getClass().getClassLoader().getResource("pknn/fxml/CreateCharacter.fxml");if (url == null) return;
AnchorPane pane = FXMLLoader.load(url);
startPanel.getChildren().setAll(pane);
Which is worked for CreateCharacter.fxml with StartController 这与带有StartController的CreateCharacter.fxml一起使用
<AnchorPane fx:id="createCharacterPanel" onMouseEntered="#paneEventHandler" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pknn.StartController">
But when I try to load another FXML to the same scene 但是当我尝试将另一个FXML加载到同一场景时
URL url = this.getClass().getClassLoader().getResource("pknn/fxml/LockerRoom.fxml");
if (url == null) return;
AnchorPane pane = FXMLLoader.load(url);
createCharacterPanel.getChildren().setAll(pane);
it doesn't work, even I change it to 它不起作用,即使我将其更改为
startPanel.getChildren().setAll(pane);
But it still not work. 但是它仍然不起作用。 This is the fxml I want to load. 这是我要加载的fxml。
<AnchorPane fx:id="lockerRoomPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pknn.LockerRoomController">
it come with so many exception like 它有很多例外
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
...
Caused by: javafx.fxml.LoadException:
/Users/pknn/Study/ComPro/MonsterBattle/out/production/MonsterBattle/pknn/fxml/LockerRoom.fxml:10
at pknn.StartController.createCharacter(StartController.java:132)
Caused by: java.lang.IllegalAccessException: Class sun.reflect.misc.ReflectUtil can not access a member of class pknn.LockerRoomController with modifiers ""
How to fix it? 如何解决? Thanks 谢谢
You cannot "override" the controller specified in the FXML. 您不能“替代” FXML中指定的控制器。 If you plan to change the controller (or have a controller with constructor parameters) then you have to remove the fx:controller
attribute from the FXML and set the controller in the FxmlLoader: 如果计划更改控制器(或具有带有构造函数参数的控制器),则必须从FXML中删除fx:controller
属性,并在FxmlLoader中设置控制器:
fxmlLoader = new FXMLLoader(fxmlFileAsResource);
fxmlLoader.setController(yourControllerInstance);
Pane pane = fxmlLoader.load();
This is an example of loading different FXML frames in the same Scene
这是在同一Scene
中加载不同的FXML框架的示例
create_character.fxml create_character.fxml
<AnchorPane fx:id="createCharacterPanel" fx:controller="sample.StartController" xmlns:fx="http://javafx.com/fxml" >
<children>
<Button text="Load" onAction="#handleLoadFXML" AnchorPane.topAnchor="0" AnchorPane.leftAnchor="0" />
<ScrollPane AnchorPane.topAnchor="30" AnchorPane.leftAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.bottomAnchor="0">
<content>
<VBox fx:id="child"/>
</content>
</ScrollPane>
</children>
</AnchorPane>
pane_a.fxml and pane_b.fxml pane_a.fxml和pane_b.fxml
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.SecondController">
<children>
<Label text="Pane A"/>
</children>
</AnchorPane>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.SecondController">
<children>
<Label text="Pane B"/>
</children>
</AnchorPane>
The controller that loads the files 加载文件的控制器
public class StartController {
@FXML
private AnchorPane createCharacterPanel;
@FXML
private VBox child;
private Parent loadFXML(String name) {
try {
return FXMLLoader.load(getClass().getResource(name));
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
@FXML
private void handleLoadFXML(ActionEvent event) {
child.getChildren().addAll(
loadFXML("pane_a.fxml"),
loadFXML("pane_b.fxml")
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.