![](/img/trans.png)
[英]JavaFX: how to change a scene included in the center of a BorderPane
[英]JavaFX: Remove the scene loaded in Center of BorderPane
我有一个JavaFX2.2应用程序,它在主屏幕上有一个BorderPane。 在顶部窗格中,我有两个按钮“按钮A”和“按钮B”,分别在BorderPane的中心动态加载场景“场景A”和“场景B”。
'场景A'有两个按钮。 其中一个在FXML文件中定义为“默认按钮”,另一个定义为“取消按钮”。
'Scene 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'并在将TextField聚焦到'场景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”的对象仍然可以访问以及为什么要为默认按钮调用事件处理程序。
这是一个已知的错误 :当按钮不是场景的一部分时,按钮不应该接收事件。 这个错误在Java 8中得到了修复。我把它的一个快速示例汇总在一起,并且可以确认Java 7中的错误并且它已在Java 8中修复。您可能希望在Java 8中运行代码并查看它是否有效正确的。
对于Java 7的变通方法,请将代码包装在defaultButton的处理程序中(也可能是取消按钮)
if (button.getScene() != null) {
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.