下面是我的以前的项目的部分。 MenuItem在不同的类中,我调用main方法来切换场景。
我有两个页面选择和信息,都有自己的容器,场景和样式表。选择页面是开始时显示的初始页面,我切换信息页面。
settingsMenuItem.setOnAction(e -> {
e.consume();
Launcher.selectionPage();
});
我的主类:
public class Launcher extends Application {
private static FlowPane selectionPane;
private static BorderPane infoPane;
private static Scene selectionScene, infoScene;
private static Stage theStage;
private static String selectionCSS;
private static String informationCSS;
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//Global reference needed to switch scenes in another method.
this.theStage = primaryStage;
//Declares resources, in this case stylesheet. Declared here to be applied in another method
selectionCSS = this.getClass().getResource("/views/SelectionStyle.css").toExternalForm();
informationCSS = this.getClass().getResource("/views/InformationStyle.css").toExternalForm();
//Initial page setup
selectionPane = new SelectionPage();
selectionScene = new Scene(selectionPane, 500, 500);
selectionScene.getStylesheets().add(selectionCSS);
//Stage setup
primaryStage.setScene(selectionScene);
primaryStage.show();
}
//Changes page
public static void informationPage(String starSign) {
infoPane = new InformationPage();
infoScene = new Scene(infoPane, 500, 270);
infoScene.getStylesheets().add(informationCSS);
theStage.setScene(infoScene);
}
}
菜单不产生事件,如果它们是空的(不幸)。大概你最好的选择是向HBox(或“ToolBar”)添加一些按钮(或者可能只是标签?),并将它们设置为菜单形式,就像你描述的那样。如果你不能按照你想要的方式工作,我建议尝试这个方法,并发布一个具体的问题,试图完成这项工作。 –