JavaFX - 選單欄



選單欄是一個圖形使用者介面元件,顯示一行水平排列的選單,每個選單包含一系列命令或選項。它位於視窗或螢幕頂部,為使用者提供了一種方便的方式來訪問應用程式的功能。一個典型的選單欄如下圖所示:

Sample Menubar

在 JavaFX 中建立選單欄

在 JavaFX 中,選單欄控制元件由名為MenuBar的類表示。此類屬於javafx.scene.control包。透過例項化此類,我們可以在 JavaFX 中建立一個 MenuBar 控制元件。

除了 MenuBar 類之外,我們還需要以下類:

Menu

Menu類表示選單欄中的單個選單。它具有一個文字屬性,用於定義其標籤,以及一個專案屬性,用於儲存選單項列表。要建立 Menu,請使用以下程式碼:

//Creating a menu
Menu objectOfMenu = new Menu("nameOfMenu");

MenuItem

MenuItem用於在選單中建立一個單個命令或選項。它具有一個文字屬性,用於定義其標籤,以及一個 onAction 屬性,用於定義使用者選擇它時發生的情況。它透過使用以下程式碼建立:

//Creating menu items for the menu
MenuItem menuItemObject = new MenuItem("nameOfMenuItem");

示例

以下示例說明如何在 JavaFX 應用程式中建立 MenuBar。將此程式碼儲存在名為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 檔案,請使用以下命令:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar

輸出

執行上述程式碼後,將生成以下輸出。

MenuBar Output

如何向選單項新增圖示?

要向選單項新增圖示,我們呼叫setGraphic()方法,該方法接受ImageView類的物件。圖示將顯示在 MenuItem 名稱旁邊。

示例

在以下 JavaFX 應用程式中,我們將演示如何在 MenuBar 中為選單項新增圖示。將此程式碼儲存在名為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 檔案:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar

輸出

執行上述 JavaFX 程式碼後,將生成以下輸出。

MenuBar Output2
廣告
© . All rights reserved.