Javafx 简明教程
JavaFX - MenuBar
menubar 是一个图形用户界面组件,它显示一行水平菜单,其中每个菜单都包含一个命令或选项列表。它位于窗口或屏幕的顶部,为用户提供了访问应用程序功能的便捷方式。一个典型的菜单栏看起来如下所示 −
A menubar is a graphical user interface component that displays a horizontal row of menus, each containing a list of commands or options. It is located at the top of a window or screen, and provides a convenient way for users to access the functionality of an application. A typical menubar looks like the below figure −
Creating MenuBar in JavaFX
在 JavaFX 中,菜单栏控件由一个名为 MenuBar 的类表示。此类属于 javafx.scene.control 包。通过实例化此类,我们可以在 JavaFX 中创建 MenuBar 控件。
In JavaFX, the menubar control is represented by a class named MenuBar. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a MenuBar control in JavaFX.
除了 MenuBar 类,我们还需要以下类 −
In addition to the MenuBar class, we also require the following classes −
Menu
Menu 类表示菜单栏中的单个菜单。它有一个 text 属性来定义其标签,以及一个 items 属性来保存菜单项列表。要创建一个 Menu,请使用下面给出的代码 −
The Menu class represents a single menu in the menubar. It has a text property that defines its label, and an items property that holds a list of menu items. To create a Menu, use the code given below −
//Creating a menu
Menu objectOfMenu = new Menu("nameOfMenu");
MenuItem
MenuItem 用于在菜单中创建单个命令或选项。它有一个 text 属性来定义其标签,以及一个 onAction 属性来定义当用户选择它时发生的情况。它使用以下代码创建 −
The MenuItem is used to create a single command or option within a menu. It has a text property that defines its label, and an onAction property that defines what happens when the user selects it. It is created by using the below code −
//Creating menu items for the menu
MenuItem menuItemObject = new MenuItem("nameOfMenuItem");
Example
以下示例说明如何在 JavaFX 应用程序中创建 MenuBar。使用名称 JavafxmenuBar.java 将此代码保存在文件中。
The following example illustrates how to create a MenuBar in JavaFX application. Save this code in a file with the name JavafxmenuBar.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;
public class JavafxmenuBar extends Application {
public void start(Stage stage) {
//Creating the first menu
Menu category = new Menu("Category");
//Creating menu items for the menu
MenuItem itemOne = new MenuItem("Programming");
MenuItem itemTwo = new MenuItem("Cyber Security");
MenuItem itemThree = new MenuItem("Marketing");
//Adding all the items to the category
category.getItems().addAll(itemOne, itemTwo, itemThree);
//Creating another menu
Menu library = new Menu("Library");
//Creating menu items for the library menu
MenuItem itemFour = new MenuItem("HTML");
MenuItem itemFive = new MenuItem("Java");
MenuItem itemSix = new MenuItem("JavaFX");
//Adding all the items to library menu
library.getItems().addAll(itemFour, itemFive, itemSix);
//Creating the third menu
Menu articles = new Menu("Articles");
//Creating menu items for the articles
MenuItem itemSeven = new MenuItem("Constructor");
MenuItem itemEight = new MenuItem("Inheritance");
MenuItem itemNine = new MenuItem("Current Affairs");
//Adding all items to the menu
articles.getItems().addAll(itemSeven, itemEight, itemNine);
//Instantiating the MenuBar class
MenuBar newmenubar = new MenuBar();
//Adding all the menus to the MenuBar object
newmenubar.getMenus().addAll(category, library, articles);
//Setting the stage
Group newgroup = new Group(newmenubar);
Scene scene = new Scene(newgroup, 600, 400);
stage.setTitle("MenuBar in JavaFX");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar
当我们执行上述代码时,它将生成以下输出。
When we execute the above code, it will generate the following output.
How to add Icons to the MenuItems?
要向菜单项添加图标,我们调用 setGraphic() 方法,该方法接受 ImageView 类的对象。图标将出现在 MenuItem 名称的旁边。
To add icons to the menuitems, we call the setGraphic() method which accepts an object of ImageView class. The icon will be appear next to the MenuItem name.
Example
在以下 JavaFX 应用程序中,我们将演示如何在 MenuBar 中的菜单项添加图标。使用名称 JavafxmenuBar.java 将此代码保存在文件中。
In the following JavaFX application, we are going to demonstrate how to add icons for the menuitems within MenuBar. Save this code in a file with the name JavafxmenuBar.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;
import java.io.File;
import javafx.scene.image.ImageView;
public class JavafxmenuBar extends Application {
public void start(Stage stage) {
//Creating the first menu
Menu category = new Menu("Category");
//Creating menu items for the menu
MenuItem itemOne = new MenuItem("Programming");
MenuItem itemTwo = new MenuItem("Cyber Security");
MenuItem itemThree = new MenuItem("Marketing");
// adding icons to the menuitems
itemOne.setGraphic(new ImageView("file:programming.png"));
itemTwo.setGraphic(new ImageView("file:cyber.png"));
itemThree.setGraphic(new ImageView("file:marketing.png"));
//Adding all the items to the category
category.getItems().addAll(itemOne, itemTwo, itemThree);
//Creating another menu
Menu library = new Menu("Library");
//Creating menu items for the library menu
MenuItem itemFour = new MenuItem("HTML");
MenuItem itemFive = new MenuItem("Java");
MenuItem itemSix = new MenuItem("JavaFX");
//Adding all the items to library menu
library.getItems().addAll(itemFour, itemFive, itemSix);
//Creating the third menu
Menu articles = new Menu("Articles");
//Creating menu items for the articles
MenuItem itemSeven = new MenuItem("Constructor");
MenuItem itemEight = new MenuItem("Inheritance");
MenuItem itemNine = new MenuItem("Current Affairs");
//Adding all items to the menu
articles.getItems().addAll(itemSeven, itemEight, itemNine);
//Instantiating the MenuBar class
MenuBar newmenubar = new MenuBar();
//Adding all the menus to the MenuBar object
newmenubar.getMenus().addAll(category, library, articles);
//Setting the stage
Group newgroup = new Group(newmenubar);
Scene scene = new Scene(newgroup, 600, 400);
stage.setTitle("MenuBar in JavaFX");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
通过使用以下命令从命令提示符编译并执行保存的 Java 文件 −
Compile and execute the saved Java file from the command prompt by using the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar
执行上述 JavaFX 代码后,它将生成以下输出。
On executing the above JavaFX code, it will generate the following output.