鍍金池/ 教程/ Java/ JavaFX菜單(Menu)
安裝e(fx)clipse到Eclipse (JavaFX工具)
JavaFX屬性
JavaFX文本域(輸入框)
JavaFX切換按鈕
JavaFX曲線
JavaFX教程
JavaFX菜單(Menu)
JavaFX快速入門
JavaFX復(fù)選框
JavaFX ScrollPane布局
JavaFX綁定
JavaFX顏色選擇器(ColorPicker)
JavaFX進(jìn)度指示器
JavaFX按鈕
JavaFX TitledPane布局
JavaFX圓弧
JavaFX開發(fā)環(huán)境安裝配置
在Eclipse安裝JavaFX Scene Builder
JavaFX路徑
JavaFX VBox
JavaFX線條
JavaFX漸變顏色
JavaFX集合
JavaFX BorderPane布局
JavaFX DatePicker
JavaFX單選按鈕
JavaFX滾動(dòng)條
JavaFX矩形橢圓
JavaFX GridPane布局
JavaFX HBox
JavaFX進(jìn)度條
JavaFX多邊形折線
JavaFX超鏈接
JavaFX密碼字段
JavaFX Accordion布局
JavaFX概述和簡(jiǎn)介
JavaFX選擇框
JavaFX文本
JavaFX顏色
JavaFX文件選擇器(FileChooser)
JavaFX標(biāo)簽
JavaFX FlowPane布局

JavaFX菜單(Menu)

菜單是桌面應(yīng)用程序選擇選項(xiàng)的標(biāo)準(zhǔn)方法。
菜單和菜單項(xiàng)可以與選擇選項(xiàng)快捷鍵組合,稱為鍵盤快捷鍵。

創(chuàng)建菜單和菜單項(xiàng)

必須創(chuàng)建一個(gè)菜單欄javafx.scene.control.MenuBar對(duì)象來保存javafx.scene.control.Menu對(duì)象。

菜單對(duì)象可以包含Menujavafx.scene.control.MenuItem對(duì)象。菜單可以包含其他菜單作為子菜單。MenuItemsMenu對(duì)象內(nèi)的子選項(xiàng)。

以下代碼顯示如何創(chuàng)建菜單欄并添加菜單和菜單項(xiàng)。

Menu類是MenuItem的子類,它有一個(gè)getItems().add()方法,它能夠添加諸如其他MenuMenuItem實(shí)例的子元素。

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    MenuBar menuBar = new MenuBar();
    menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
    root.setTop(menuBar);

    // File menu - new, save, exit
    Menu fileMenu = new Menu("File");
    MenuItem newMenuItem = new MenuItem("New");
    MenuItem saveMenuItem = new MenuItem("Save");
    MenuItem exitMenuItem = new MenuItem("Exit");
    exitMenuItem.setOnAction(actionEvent -> Platform.exit());

    fileMenu.getItems().addAll(newMenuItem, saveMenuItem,
        new SeparatorMenuItem(), exitMenuItem);

    Menu webMenu = new Menu("Web");
    CheckMenuItem htmlMenuItem = new CheckMenuItem("HTML");
    htmlMenuItem.setSelected(true);
    webMenu.getItems().add(htmlMenuItem);

    CheckMenuItem cssMenuItem = new CheckMenuItem("CSS");
    cssMenuItem.setSelected(true);
    webMenu.getItems().add(cssMenuItem);

    Menu sqlMenu = new Menu("SQL");
    ToggleGroup tGroup = new ToggleGroup();
    RadioMenuItem mysqlItem = new RadioMenuItem("MySQL");
    mysqlItem.setToggleGroup(tGroup);

    RadioMenuItem oracleItem = new RadioMenuItem("Oracle");
    oracleItem.setToggleGroup(tGroup);
    oracleItem.setSelected(true);

    sqlMenu.getItems().addAll(mysqlItem, oracleItem,
        new SeparatorMenuItem());

    Menu tutorialManeu = new Menu("Tutorial");
    tutorialManeu.getItems().addAll(
        new CheckMenuItem("Java"),
        new CheckMenuItem("JavaFX"),
        new CheckMenuItem("Swing"));

    sqlMenu.getItems().add(tutorialManeu);

    menuBar.getMenus().addAll(fileMenu, webMenu, sqlMenu);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
  public static void main(String[] args) {
    launch(args);
  }
}

上面的代碼生成以下結(jié)果。

指定菜單項(xiàng)

要將選中的選項(xiàng)或單選按鈕添加到菜單,可以使用MenuItem類作為菜單的子類。以下列表顯示了可用作菜單選項(xiàng)的MenuItem子類。

  • javafx.scene.control.CheckMenuItem
  • javafx.scene.control.RadioMenuItem
  • javafx.scene.control.CustomMenuItem
  • javafx.scene.control.SeparatorMenuItem
  • javafx.scene.control.Menu

CheckMenuItem菜單項(xiàng)類似于復(fù)選框控件,允許用戶選擇項(xiàng)。
RadioMenuItem菜單項(xiàng)類似于RadioButton控件,允許用戶從項(xiàng)目組中僅選擇一個(gè)項(xiàng)目。

要?jiǎng)?chuàng)建自定義菜單項(xiàng),可以使用CustomMenuItem類。SeparatorMenuItemCustomMenuItem類型的派生類,并顯示用于分隔菜單項(xiàng)的視線。使用CustomMenuItemSlider添加到MenuItem上??蓞⒖家韵麓a實(shí)現(xiàn) -

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Slider;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Menus");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);

        MenuBar menuBar = new MenuBar();

        Menu menu = new Menu("File");
        menu.getItems().add(new MenuItem("New"));
        menu.getItems().add(new MenuItem("Save"));
        menu.getItems().add(new SeparatorMenuItem());
        menu.getItems().add(new MenuItem("Exit"));

        CustomMenuItem customMenuItem = new CustomMenuItem(new Slider());
        customMenuItem.setHideOnClick(false);
        menu.getItems().add(customMenuItem);

        menuBar.getMenus().add(menu);

        menuBar.prefWidthProperty().bind(primaryStage.widthProperty());

        root.getChildren().add(menuBar); 
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

上面的代碼生成以下結(jié)果。

菜單事件處理程序

要向菜單項(xiàng)添加事件處理程序,可以使用setOnAction()方法,它接收一個(gè)類型為EventHandler <ActionEvent>的功能接口,它是在選擇菜單項(xiàng)時(shí)調(diào)用的處理程序代碼。

import javafx.application.Application;
import javafx.application.Platform;
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.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    MenuBar menuBar = new MenuBar();
    menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
    root.setTop(menuBar);

    Menu fileMenu = new Menu("File");
    MenuItem exitMenuItem = new MenuItem("Exit");
    fileMenu.getItems().add(exitMenuItem);
    exitMenuItem.setOnAction(actionEvent -> Platform.exit());

    menuBar.getMenus().addAll(fileMenu);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
  public static void main(String[] args) {
    launch(args);
  }
}

上面的代碼生成以下結(jié)果。

關(guān)鍵助記符

標(biāo)準(zhǔn)菜單通常具有鍵盤助記符(類似于快捷鍵),以在不使用鼠標(biāo)的情況下選擇菜單項(xiàng)。
用戶可以按Alt鍵和帶下劃線_的字母來激活菜單,然后使用箭頭鍵導(dǎo)航。要向菜單添加鍵助記符,使用String值調(diào)用構(gòu)造函數(shù),并在菜單或菜單項(xiàng)的文本中在所選字母前面放置一個(gè)下劃線字符。

然后將true傳遞給setMnemonicParsing(true)方法。

以下代碼創(chuàng)建一個(gè)使用字母“F”作為助記符的文件菜單。

Menu fileMenu = new Menu("_File");
fileMenu.setMnemonicParsing(true);

完整的源代碼實(shí)現(xiàn),如下所示 -

import javafx.application.Application;
import javafx.application.Platform;
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.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    MenuBar menuBar = new MenuBar();
    menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
    root.setTop(menuBar);

    Menu fileMenu = new Menu("_File");
    fileMenu.setMnemonicParsing(true);
    MenuItem exitMenuItem = new MenuItem("Exit");
    fileMenu.getItems().add(exitMenuItem);
    exitMenuItem.setOnAction(actionEvent -> Platform.exit());

    menuBar.getMenus().addAll(fileMenu);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
  public static void main(String[] args) {
    launch(args);
  }
}

上面的代碼生成以下結(jié)果(同時(shí)按住Ctrl + ALT + F)。

鍵組合

鍵組合是用于選擇菜單選項(xiàng)的鍵擊的組合。鍵組合稱為鍵盤快捷鍵。

例如,在Windows平臺(tái)上,Ctrl + S的組合鍵可以保存文件。 在Mac OS平臺(tái)上,組合鍵為Command + S.

Ctrl,Command,AltShiftMeta等鍵稱為修飾鍵。

通常,修飾符與單個(gè)字母組合使用。要?jiǎng)?chuàng)建一個(gè)組合鍵,使用KeyCodeCombination對(duì)象,并傳入擊鍵和修飾符。

以下代碼創(chuàng)建(CtrlMeta)+ S的鍵代碼組合。

MenuItem saveItem = new MenuItem("_Save");
saveItem.setMnemonicParsing(true);
saveItem.setAccelerator(new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN));

代碼使用KeyCombination.SHORTCUT_DOWN值作為鍵修飾符,而不是CONTROL_DOWNMETA_DOWN。SHORTCUT_DOWN值將使應(yīng)用程序能夠跨平臺(tái)使用。

CONTROL_DOWNMETA_DOWN值分別依賴于Windows和MacOS平臺(tái),但是SHORTCUT_DOWN在所有平臺(tái)上都有效。

上下文菜單

上下文菜單是當(dāng)用戶右鍵單擊鼠標(biāo)按鈕時(shí)顯示的彈出菜單。

要?jiǎng)?chuàng)建上下文菜單,請(qǐng)使用ContextMenu類。ContextMenu菜單有一個(gè)getItems().add()方法來添加菜單項(xiàng)。

以下代碼顯示了使用菜單項(xiàng)(exitItem)實(shí)例化的上下文菜單:

ContextMenu  contextFileMenu = new ContextMenu(exitItem);

要響應(yīng)鼠標(biāo)右鍵單擊,請(qǐng)?zhí)砑右粋€(gè)事件處理程序,以監(jiān)聽右鍵單擊事件并調(diào)用上下文菜單的show()方法。

以下代碼設(shè)置了一個(gè)事件處理程序,以分別基于右鍵或左鍵單擊來顯示和隱藏上下文菜單。

hide()方法由主鼠標(biāo)單擊(左鍵單擊)調(diào)用以刪除上下文菜單。

primaryStage.addEventHandler(MouseEvent.MOUSE_CLICKED,  (MouseEvent  me) ->  {
    if (me.getButton() == MouseButton.SECONDARY  || me.isControlDown())  {
        contextFileMenu.show(root, me.getScreenX(), me.getScreenY());
    }  else  {
        contextFileMenu.hide();
    }
});

上一篇:JavaFX HBox下一篇:JavaFX TitledPane布局