菜單是桌面應(yīng)用程序選擇選項(xiàng)的標(biāo)準(zhǔn)方法。
菜單和菜單項(xiàng)可以與選擇選項(xiàng)快捷鍵組合,稱為鍵盤快捷鍵。
必須創(chuàng)建一個(gè)菜單欄javafx.scene.control.MenuBar
對(duì)象來保存javafx.scene.control.Menu
對(duì)象。
菜單對(duì)象可以包含Menu
和javafx.scene.control.MenuItem
對(duì)象。菜單可以包含其他菜單作為子菜單。MenuItems
是Menu
對(duì)象內(nèi)的子選項(xiàng)。
以下代碼顯示如何創(chuàng)建菜單欄并添加菜單和菜單項(xiàng)。
Menu
類是MenuItem
的子類,它有一個(gè)getItems().add()
方法,它能夠添加諸如其他Menu
和MenuItem
實(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)或單選按鈕添加到菜單,可以使用MenuItem
類作為菜單的子類。以下列表顯示了可用作菜單選項(xiàng)的MenuItem
子類。
CheckMenuItem
菜單項(xiàng)類似于復(fù)選框控件,允許用戶選擇項(xiàng)。RadioMenuItem
菜單項(xiàng)類似于RadioButton
控件,允許用戶從項(xiàng)目組中僅選擇一個(gè)項(xiàng)目。
要?jiǎng)?chuàng)建自定義菜單項(xiàng),可以使用CustomMenuItem
類。SeparatorMenuItem
是CustomMenuItem
類型的派生類,并顯示用于分隔菜單項(xiàng)的視線。使用CustomMenuItem
將Slider
添加到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é)果。
標(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
,Alt
,Shift
和Meta
等鍵稱為修飾鍵。
通常,修飾符與單個(gè)字母組合使用。要?jiǎng)?chuàng)建一個(gè)組合鍵,使用KeyCodeCombination
對(duì)象,并傳入擊鍵和修飾符。
以下代碼創(chuàng)建(Ctrl
或Meta
)+ S
的鍵代碼組合。
MenuItem saveItem = new MenuItem("_Save");
saveItem.setMnemonicParsing(true);
saveItem.setAccelerator(new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN));
代碼使用KeyCombination.SHORTCUT_DOWN
值作為鍵修飾符,而不是CONTROL_DOWN
或META_DOWN
。SHORTCUT_DOWN
值將使應(yīng)用程序能夠跨平臺(tái)使用。
CONTROL_DOWN
和META_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();
}
});