我想开发一个多场景Java FX应用程序。但是我想在所有场景中都有通用的菜单。我认为使用FXML可以在场景中创建菜单。
如果是这样,那是怎么回事呢?否则,让我知道它的任何替代方案。
发布于 2013-03-03 17:36:27
是的,这是可能的。我在自己的应用程序中使用了这种机制。
我首先要做的是创建一个带有菜单栏的FXML和一个包含内容的AnchorPane。此FXML在应用程序启动时加载。
我使用了一个上下文类(基于谢尔盖在这个问题中的回答:Multiple FXML with Controllers, share object),它包含一个方法ShowContentPane(String url)方法:
public void showContentPane(String sURL){
try {
getContentPane().getChildren().clear();
URL url = getClass().getResource(sURL);
//this method returns the AnchorPane pContent
AnchorPane n = (AnchorPane) FXMLLoader.load(url, ResourceBundle.getBundle("src.bundles.bundle", getLocale()));
AnchorPane.setTopAnchor(n, 0.0);
AnchorPane.setBottomAnchor(n, 0.0);
AnchorPane.setLeftAnchor(n, 0.0);
AnchorPane.setRightAnchor(n, 0.0);
getContentPane().getChildren().add(n);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}所以基本上发生的是:
程序启动时,在Context中设置content窗格:
@Override
public void initialize(URL url, ResourceBundle rb) {
Context.getInstance().setContentPane(pContent); //pContent is the name of the AnchorPane containing the content
...
}当选择一个按钮或菜单项时,我在内容窗格中加载FXML:
@FXML
private void handle_FarmerListButton(ActionEvent event) {
Context.getInstance().showContentPane("/GUI/user/ListUser.fxml");
}希望这能有所帮助:)
https://stackoverflow.com/questions/15179299
复制相似问题