查看:
public class View extends Application {
private Stage stage; // A main window
private Scene scene; // Content in main window
private int width = 640, height = 480;
private BorderPane root;
private MenuBar menuBar;
private Menu menuFile, menuEdit, menuView, menuHelp, menuAbout;
public MenuItem Open, Save, Close, Exit, Copy, Paste, Maximise, Minimise, Docs, Version;
@Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
init();
root = new BorderPane();
Label label = new Label("Main Screen");
root.setCenter(label);
root.setTop(menuBar);
scene = new Scene(root, width, height);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stage.setScene(scene);
stage.setTitle("Title");
stage.show();
}
public static void main (String[] args) {
Application.launch(args);
}
public void init() {
menuBar = new MenuBar();
menuFile = new Menu("File");
menuEdit = new Menu("Edit");
menuView = new Menu("View");
menuHelp = new Menu("Help");
menuAbout = new Menu("About");
Open = new MenuItem("Open");
Save = new MenuItem("Save");
Close = new MenuItem("Close");
Exit = new MenuItem("Exit");
Copy = new MenuItem("Copy");
Paste = new MenuItem("Paste");
Maximise = new MenuItem("Maximise");
Minimise = new MenuItem("Minimise");
Docs = new MenuItem("Docs");
Version = new MenuItem("Version");
menuFile.getItems().addAll(Open, Save, Close, Exit);
menuEdit.getItems().addAll(Copy, Paste);
menuView.getItems().addAll(Maximise, Minimise);
menuHelp.getItems().addAll(Docs);
menuAbout.getItems().addAll(Version);
menuBar.getMenus().addAll(menuFile, menuEdit, menuView, menuHelp, menuAbout);
}
public void MenuItemListener(MenuItem menuItem, EventHandler<ActionEvent> handler) {
menuItem.setOnAction(handler);
}
public Scene getScene() {
return scene;
}
public void setScene(Scene scene) {
this.scene = scene;
}
public Stage getStage() {
return stage;
}
public void setStage(Stage stage) {
this.stage = stage;
}
}
控制器:
public class Controller {
private View view;
private Model model;
Controller(View v, Model m) {
this.view = v;
this.model = m;
this.view.MenuItemListener(this.view.Exit, handler -> {
System.exit(0);
});
this.view.MenuItemListener(this.view.Maximise, handler -> {
view.getStage().fullScreenProperty();
});
this.view.MenuItemListener(this.view.Minimise, handler -> {
view.getStage().setIconified(true);
});
}
}