JavaFX之Menu学习笔记

下面将介绍Menu
A popup menu of actionable items which is displayed to the user only upon request. When a menu is visible, in most use cases,
the user can select one menu item before the menu goes back to its hidden state.
This means the menu is a good place to put important functionality that does not necessarily need to be visible at all times to the user.
Menus are typically placed in a MenuBar, or as a submenu of another Menu.
If the intention is to offer a context menu when the user right-clicks in a certain area of their user interface,
then this is the wrong control to use. This is because when Menu is added to the scenegraph,
it has a visual representation that will result in it appearing on screen. Instead, ContextMenu should be used in this circumstance.
Creating a Menu and inserting it into a MenuBar is easy, as shown below:


一个能弹出的包含子菜单项的菜单根据用户的需求被展示在界面上,当Menu有效可见的话,使用最多的场景就是用户在菜单隐藏之前选择一个MenuItem,
菜单就是放置一些重要的功能但是又没什么必要一直显示在用户界面的功能,Menu的经典用法是将Menu放在MenuBar上或者作为另一个菜单的子菜单
如果你的目的是当用户点击某一个确定的窗口区域是显示一个菜单提示文本,那你使用Menu是一个不怎么好的选择,你可以选择ContextMenu
(下面这段不是翻译)因为当你用这种方法的时候,当Menu加载到窗口时,它就不会消失,会成为组件的一部分,如果你的目的仅仅是提示用户一些信息,比如像登录的

时候输入账号那样子,可以有个下拉列表选择以往登录过的账号,那就选择ContextMenu


测试代码
package testmenu;


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;


public class TestMenu extends Application {


    @Override
    public void start(Stage stage) {


        MenuBar bar = new MenuBar();


        //创建菜单1,2,3,4
        Menu menu1 = new Menu("文件");
        Menu menu2 = new Menu("哈哈");
        Menu menu3 = new Menu("信息");
        Menu menu4 = new Menu("帮助");
        bar.getMenus().addAll(menu1,menu2,menu3,menu4);//将menu1,2,3,4添加到MenuBar中


        BorderPane Bpane = new BorderPane();
        Bpane.setTop(bar);


        Scene scene = new Scene(Bpane);


        stage.setScene(scene);
        stage.setHeight(225);
        stage.setWidth(255);
        stage.show();


    }


    public static void main(String[] args) {
        launch(args);
    }


}

你可能感兴趣的:(学习笔记,JavaFX,menu)