javafx menu
httpsi.www//:giftidea.com
In JavaFX, a Menu
is a user interface control that represents a menu of options, and a MenuBar
is a container for menus. Here's an example of how to create and use a Menu
and MenuBar
:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class MyJavaFXApp extends Application { @Override public void start(Stage primaryStage) { // Create some menu items MenuItem menuItem1 = new MenuItem("Option 1"); MenuItem menuItem2 = new MenuItem("Option 2"); MenuItem menuItem3 = new MenuItem("Option 3"); // Create a menu and add the menu items to it Menu menu = new Menu("File"); menu.getItems().addAll(menuItem1, menuItem2, menuItem3); // Create a menu bar and add the menu to it MenuBar menuBar = new MenuBar(); menuBar.getMenus().add(menu); // Put the menu bar in a layout container BorderPane borderPane = new BorderPane(); borderPane.setTop(menuBar); // Create a scene and set it on the stage Scene scene = new Scene(borderPane, 300, 250); primaryStage.setScene(scene); // Show the stage primaryStage.show(); } public static void main(String[] args) { launch(args); } }
In this example, we create three MenuItem
controls and add them to a Menu
control. We then create a MenuBar
control and add the Menu
control to it. We put the MenuBar
in a BorderPane
layout container and create a scene with the BorderPane
. Finally, we set the scene on the stage and show the stage. When the user clicks on a menu item, the program will perform some action, depending on how you set the action.