我有一个JavaFX2.2应用程序,它在主屏幕上有一个BorderPane。在顶部窗格中,我有两个按钮“按钮A”和“按钮B”分别动态加载BorderPane中心的场景“场景A”和“场景B”。JavaFX:删除加载在BorderPane中心的场景
'场景A'有两个按钮。其中一个定义为'默认按钮',另一个定义为FXML文件中的'取消按钮'。
'场景B'有一个TextField和一个TableView。
以下是主屏幕之间切换场景的代码片段。
@FXML
private void handlebtnAAction(ActionEvent event) {
loadCentreScene("fxml/FXSceneA.fxml");
}
@FXML
private void handlebtnBAction(ActionEvent event) {
LoadCentreScene("fxml/FXSceneB.fxml");
}
private void loadCentreScene(String fxmlPath){
try {
FXMLLoader loader = new FXMLLoader(Admin.class.getResource(fxmlPath));
AnchorPane page = (AnchorPane) loader.load();
Plugin fxController = loader.getController();
fxController.setMainController(this);
Node node = getRootLayout().getCenter();
node = null;
getRootLayout().setCenter(page);
} catch (IOException ex) {
Dialogs.showErrorDialog(primaryStage, ex.getMessage(), "Loading Error");
}
}
现在,当我从“场景A”到“场景B”开关和打击重点“场景B”,事件处理程序上的“场景A”的默认按钮的文本字段后,“确认”按钮时,执行。
我也尝试了以下变化,但我仍然面临同样的问题。
private void loadCentreScene(String fxmlPath){
try {
FXMLLoader loader = new FXMLLoader(Admin.class.getResource(fxmlPath));
AnchorPane page = (AnchorPane) loader.load();
Plugin fxController = loader.getController();
fxController.setMainController(this);
Node node = getRootLayout().getCenter();
getRootLayout().getChildren().remove(node); //<****Remove the node from children****>
getRootLayout().setCenter(null); //<****Set center to null****>
node = null;
getRootLayout().setCenter(page);
} catch (IOException ex) {
Dialogs.showErrorDialog(primaryStage, ex.getMessage(), "Loading Error");
}
}
根据我的理解,该对象应该是无法访问,并在稍后收集垃圾。任何人都可以帮助我理解为什么'场景A'的对象仍然可以访问,并且为什么事件处理程序正在为默认按钮调用。