我测试了在 JavaFX 中创建简单的对话框,但由于某种原因代码不起作用:
MenuBar menuBar = new MenuBar();
// File menu - new, save, exit
Menu menu = new Menu("File");
menu.getItems().add(new MenuItem("New"));
menu.getItems().add(new MenuItem("Save"));
menu.getItems().add(new SeparatorMenuItem());
menuBar.getMenus().add(menu);
// Options menu - Preferences
Menu options = new Menu("Options");
options.getItems().add(new MenuItem("Preferences"));
menuBar.getMenus().add(options);
// Help menu - About
Menu help = new Menu("Help");
MenuItem about = new MenuItem("Exit");
about.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create().
children(new Text("Hi"), new Button("Ok.")).
alignment(Pos.CENTER).padding(new Insets(5)).build()));
dialogStage.show();
}
});
menuBar.getMenus().add(help);
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
我希望在单击About
菜单项时显示带有作者信息的简单对话框窗口。你能告诉我如何纠正我的错误吗?