Revision: 69557
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 16, 2015 14:31 by lasagna7355608
Initial Code
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyCombination; import javafx.scene.input.MouseButton; import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.BorderPane; import javafx.scene.layout.CornerRadii; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.paint.CycleMethod; import javafx.scene.paint.LinearGradient; import javafx.scene.paint.Stop; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class MenuExample extends Application { public static void main(String[] args) { launch(args); } private Stage stage = null; public Menu createFileMenu() { Menu fileMenu = new Menu("File"); Image icon = new Image(Example01.class.getResourceAsStream("/images/new.png")); MenuItem itemNew = new MenuItem("New...", new ImageView(icon)); itemNew.setAccelerator(KeyCombination.keyCombination("Ctrl+N")); itemNew.setOnAction(event -> System.out.println("New... menu item clicked.")); // create open menu item MenuItem itemOpen = new MenuItem("Open..."); itemOpen.setOnAction(event -> System.out.println("Open... menu item clicked.")); // create exit menu item MenuItem itemExit = new MenuItem("Exit"); itemExit.setAccelerator(KeyCombination.keyCombination("Ctrl+X")); itemExit.setOnAction(event -> { System.out.println("Exit menu item clicked."); // stop the application stage.hide(); }); // add menu items to the menu fileMenu.getItems().addAll( itemNew, itemOpen, itemExit ); return fileMenu; } public Menu createEditMenu() { Menu editMenu = new Menu("Edit"); MenuItem itemCopy = new MenuItem("Copy"); itemCopy.setOnAction(event -> System.out.println("Copy menu item clicked.")); MenuItem itemCut = new MenuItem("Cut"); itemCut.setOnAction(event -> System.out.println("Cut menu item clicked.")); // add menu items to the menu editMenu.getItems().addAll( itemCopy, itemCut, createPasteMenu() ); return editMenu; } public Menu createPasteMenu() { Menu pasteMenu = new Menu("Paste as"); MenuItem itemPasteAsText = new MenuItem("Text"); itemPasteAsText.setOnAction(event -> System.out.println("Paste as Text menu item clicked.")); MenuItem itemPasteAsContent = new MenuItem("Content"); itemPasteAsContent.setOnAction(event -> System.out.println("Paste as Content menu item clicked.")); MenuItem itemPasteAsFormatting = new MenuItem("Formatting"); itemPasteAsFormatting.setOnAction(event -> System.out.println("Paste as Formatting menu item clicked.")); // add menu items to the menu pasteMenu.getItems().addAll( itemPasteAsText, itemPasteAsContent, itemPasteAsFormatting ); return pasteMenu; } public MenuBar createMenuBar() { MenuBar menuBar = new MenuBar(); menuBar.getMenus().addAll( createFileMenu(), createEditMenu() ); return menuBar; } public ContextMenu createContextMenu() { ContextMenu contextMenu = new ContextMenu( createFileMenu(), createEditMenu() ); return contextMenu; } public Scene createScene() { BorderPane bp = new BorderPane(); VBox center = new VBox(); center.setSpacing(10); Rectangle rect = createRectangle(); center.getChildren().addAll( createMenuButton(), createSplitMenuButton(), rect ); // make the context menu appear when right-click on the center VBox ContextMenu contextMenu = createContextMenu(); rect.setOnMousePressed(event -> { if (event.getButton() == MouseButton.SECONDARY) { contextMenu.show(stage, event.getScreenX(), event.getScreenY()); } }); // set the menu bar to the top section, rest in center bp.setTop(createMenuBar()); bp.setCenter(center); return new Scene(bp, 500, 400); } @Override public void start(Stage primaryStage) throws Exception { stage = primaryStage; primaryStage.setTitle("Example 1"); primaryStage.setScene(createScene()); primaryStage.show(); } }
Initial URL
manualmenujava
Initial Description
Cumbersome but effective code to creating a menu bar
Initial Title
Manually Creating a Menu Bar in Java
Initial Tags
Initial Language
Java