GWT - 選單欄小部件



介紹

MenuBar 小部件表示標準的選單欄小部件。選單欄可以包含任意數量的選單項,每個選單項可以觸發命令或開啟級聯選單欄。

類宣告

以下是 com.google.gwt.user.client.ui.MenuBar 類的宣告:

public class MenuBar
   extends Widget
      implements PopupListener, HasAnimation, 
	     HasCloseHandlers<PopupPanel>

CSS 樣式規則

以下預設 CSS 樣式規則將應用於所有 MenuBar 小部件。您可以根據需要覆蓋它。

.gwt-MenuBar {}

.gwt-MenuBar-horizontal {}

.gwt-MenuBar-vertical{}

.gwt-MenuBar .gwt-MenuItem {}

.gwt-MenuBar .gwt-MenuItem-selected {}

.gwt-MenuBar .gwt-MenuItemSeparator {}

.gwt-MenuBar .gwt-MenuItemSeparator .menuSeparatorInner {}

.gwt-MenuBarPopup .menuPopupTopLeft {}

.gwt-MenuBarPopup .menuPopupTopLeftInner {}

.gwt-MenuBarPopup .menuPopupTopCenter {}

.gwt-MenuBarPopup .menuPopupTopCenterInner {}

.gwt-MenuBarPopup .menuPopupTopRight {}

.gwt-MenuBarPopup .menuPopupTopRightInner {}

.gwt-MenuBarPopup .menuPopupMiddleLeft {}

.gwt-MenuBarPopup .menuPopupMiddleLeftInner {}

.gwt-MenuBarPopup .menuPopupMiddleCenter {}

.gwt-MenuBarPopup .menuPopupMiddleCenterInner {}

.gwt-MenuBarPopup .menuPopupMiddleRight {}

.gwt-MenuBarPopup .menuPopupMiddleRightInner {}

.gwt-MenuBarPopup .menuPopupBottomLeft {}

.gwt-MenuBarPopup .menuPopupBottomLeftInner {}

.gwt-MenuBarPopup .menuPopupBottomCenter {}

.gwt-MenuBarPopup .menuPopupBottomCenterInner {}

.gwt-MenuBarPopup .menuPopupBottomRight {}

.gwt-MenuBarPopup .menuPopupBottomRightInner {}

類建構函式

序號 建構函式和描述
1

MenuBar()

建立一個空的水平選單欄。

2

MenuBar(boolean vertical)

建立一個空的選單欄。

3

MenuBar(boolean vertical, MenuBar.MenuBarImages images)

已棄用。已替換為 MenuBar(boolean, Resources)

4

MenuBar(boolean vertical, MenuBar.Resources resources)

建立一個使用指定 ClientBundle 獲取選單影像的空選單欄。

5

MenuBar(MenuBar.MenuBarImages images)

已棄用。已替換為 MenuBar(Resources)

6

MenuBar(MenuBar.Resources resources)

建立一個使用指定 ClientBundle 獲取選單影像的空水平選單欄。

類方法

序號 函式名稱和描述
1

HandlerRegistration addCloseHandler(CloseHandler<PopupPanel> handler)

新增 CloseEvent 處理程式。

2

MenuItem addItem(MenuItem item)

向欄中新增選單項。

3

MenuItem addItem(SafeHtml html, Command cmd)

向欄中新增包含 SafeHtml 的選單項,當選中該項時將觸發給定的命令。

4

MenuItem addItem(SafeHtml html, MenuBar popup)

向欄中新增選單項,當選中該項時將開啟指定的選單。

5

MenuItem addItem(java.lang.String text, boolean asHTML, Command cmd)

向欄中新增選單項,當選中該項時將觸發給定的命令。

6

MenuItem addItem(java.lang.String text, boolean asHTML, MenuBar popup)

向欄中新增選單項,當選中該項時將開啟指定的選單。

7

MenuItem addItem(java.lang.String text, Command cmd)

向欄中新增選單項,當選中該項時將觸發給定的命令。

8

MenuItem addItem(java.lang.String text, MenuBar popup)

向欄中新增選單項,當選中該項時將開啟指定的選單。

9

MenuItemSeparator addSeparator()

向 MenuBar 新增一條細線以分隔 MenuItem 部分。

10

MenuItemSeparator addSeparator(MenuItemSeparator separator)

向 MenuBar 新增一條細線以分隔 MenuItem 部分。

11

void clearItems()

從此選單欄中刪除所有選單項。

12

void closeAllChildren(boolean focus)

關閉此選單和所有子選單彈出視窗。

13

void focus()

使此 MenuBar 獲得焦點。

14

boolean getAutoOpen()

獲取此選單欄的子選單是否會在滑鼠移過它時開啟。

15

int getItemIndex(MenuItem item)

獲取 MenuItem 的索引。

16

protected java.util.ListgetItems()

返回一個列表,其中包含選單欄中的 MenuItem 物件。

17

protected MenuItem getSelectedItem()

返回當前由使用者選中(突出顯示)的 MenuItem。

18

int getSeparatorIndex(MenuItemSeparator item)

獲取 MenuItemSeparator 的索引。

19

MenuItem insertItem(MenuItem item, int beforeIndex)

在特定索引處向欄中新增選單項。

20

MenuItemSeparator insertSeparator(int beforeIndex)

在指定索引處向 MenuBar 新增一條細線以分隔 MenuItem 部分。

21

MenuItemSeparator insertSeparator(MenuItemSeparator separator, int beforeIndex)

在指定索引處向 MenuBar 新增一條細線以分隔 MenuItem 部分。

22

boolean isAnimationEnabled()

如果啟用了動畫,則返回 true,否則返回 false。

23

boolean isFocusOnHoverEnabled()

檢查此小部件在滑鼠懸停在其上時是否會竊取鍵盤焦點。

24

void moveSelectionDown()

將選單選擇向下移動到下一項。

25

void moveSelectionUp()

將選單選擇向上移動到上一項。

26

void onBrowserEvent(Event event)

每當收到瀏覽器事件時觸發。

27

protected void onDetach()

當小部件從瀏覽器的文件中分離時,將呼叫此方法。

28

protected void onEnsureDebugId(java.lang.String baseID)

受影響的元素:-item# = 指定索引處的 MenuItem。

29

void onPopupClosed(PopupPanel sender, boolean autoClosed)

已棄用。請改用 addCloseHandler(CloseHandler)

30

void removeItem(MenuItem item)

從欄中刪除指定的選單項。

31

void removeSeparator(MenuItemSeparator separator)

從欄中刪除指定的 MenuItemSeparator。

32

void selectItem(MenuItem item)

選擇給定的 MenuItem,它必須是此 MenuBar 的直接子項。

33

void setAnimationEnabled(boolean enable)

啟用或停用動畫。

34

void setAutoOpen(boolean autoOpen)

設定此選單欄的子選單是否會在滑鼠移過它時開啟。

35

void setFocusOnHoverEnabled(boolean enabled)

啟用或停用滑鼠懸停在 MenuBar 上時的自動聚焦。

繼承的方法

此類繼承自以下類的方法:

  • com.google.gwt.user.client.ui.UIObject

  • com.google.gwt.user.client.ui.Widget

  • java.lang.Object

MenuBar 小部件示例

此示例將引導您完成簡單的步驟,以演示如何在 GWT 中使用 MenuBar 小部件。請按照以下步驟更新我們在GWT - 建立應用程式章節中建立的 GWT 應用程式:

步驟 描述
1 GWT - 建立應用程式章節中所述,建立一個名為HelloWorld的專案,該專案位於com.tutorialspoint包下。
2 修改HelloWorld.gwt.xmlHelloWorld.cssHelloWorld.htmlHelloWorld.java,如下所述。保持其餘檔案不變。
3 編譯並執行應用程式以驗證已實現邏輯的結果。

以下是修改後的模組描述符src/com.tutorialspoint/HelloWorld.gwt.xml的內容。

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'helloworld'>
   <!-- Inherit the core Web Toolkit stuff.                        -->
   <inherits name = 'com.google.gwt.user.User'/>

   <!-- Inherit the default GWT style sheet.                       -->
   <inherits name = 'com.google.gwt.user.theme.clean.Clean'/>

   <!-- Specify the app entry point class.                         -->
   <entry-point class = 'com.tutorialspoint.client.HelloWorld'/>

   <!-- Specify the paths for translatable code                    -->
   <source path = 'client'/>
   <source path = 'shared'/>

</module>

以下是修改後的樣式表文件war/HelloWorld.css的內容。

body {
   text-align: center;
   font-family: verdana, sans-serif;
}

h1 {
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-align: center;
}

.gwt-MenuBar {
   cursor: default;  
}

.gwt-MenuBar .gwt-MenuItem {
   cursor: default;
   font-family: Arial Unicode MS, Arial, sans-serif;
   font-size: 12px;
}

.gwt-MenuBar .gwt-MenuItem-selected {
   background: #E3E8F3;
}

.gwt-MenuBar-horizontal {
   background: #e3e8f3 url(images/hborder.png) repeat-x 0px -2003px;
   border: 1px solid #e0e0e0;
}

.gwt-MenuBar-horizontal .gwt-MenuItem {
   padding: 5px 10px;
   vertical-align: bottom;
   color: #000;
   font-weight: bold;  
}

.gwt-MenuBar-horizontal .gwt-MenuItemSeparator {
   width: 1px;
   padding: 0px;
   margin: 0px;
   border: 0px;
   border-left: 1px solid #ccc;
   background: white;
}

.gwt-MenuBar-horizontal .gwt-MenuItemSeparator .menuSeparatorInner {
   width: 1px;
   height: 1px;
   background: white; 
}

.gwt-MenuBar-vertical {
   margin-top: 0px;
   margin-left: 0px;
   background: white;
}

.gwt-MenuBar-vertical table {
   border-collapse: collapse;
}

.gwt-MenuBar-vertical .gwt-MenuItem {
   padding: 2px 40px 2px 1px;
}

.gwt-MenuBar-vertical .gwt-MenuItemSeparator {
   padding: 2px 0px;
}

.gwt-MenuBar-vertical .gwt-MenuItemSeparator .menuSeparatorInner {
   height: 1px;
   padding: 0px;
   border: 0px;
   border-top: 1px solid #ccc;
   overflow: hidden;
}

.gwt-MenuBar-vertical .subMenuIcon {
   padding-right: 4px;
}

.gwt-MenuBar-vertical .subMenuIcon-selected {
   background: #E3E8F3;
}

.gwt-MenuBarPopup {
   margin: 0px 0px 0px 3px;
}

.gwt-MenuBarPopup .menuPopupTopLeftInner {
   width: 5px;
   height: 5px;
   zoom: 1;
}

.gwt-MenuBarPopup .menuPopupTopRightInner {
   width: 8px;
   height: 5px;
   zoom: 1;
}

.gwt-MenuBarPopup .menuPopupBottomLeftInner {
   width: 5px;
   height: 8px;
   zoom: 1;
}

.gwt-MenuBarPopup .menuPopupBottomRightInner {
   width: 8px;
   height: 8px;
   zoom: 1;
}

.gwt-MenuBarPopup .menuPopupTopLeft {
   background: url(images/corner.png) no-repeat 0px -36px;
   -background: url(images/corner_ie6.png) no-repeat 0px -36px;
}

.gwt-MenuBarPopup .menuPopupTopRight {
   background: url(images/corner.png) no-repeat -5px -36px;
   -background: url(images/corner_ie6.png) no-repeat -5px -36px;
}

.gwt-MenuBarPopup .menuPopupBottomLeft {
   background: url(images/corner.png) no-repeat 0px -41px;
   -background: url(images/corner_ie6.png) no-repeat 0px -41px;
}

.gwt-MenuBarPopup .menuPopupBottomRight {
   background: url(images/corner.png) no-repeat -5px -41px;
   -background: url(images/corner_ie6.png) no-repeat -5px -41px;
}

html > body .gwt-MenuBarPopup {
}

* html .gwt-MenuBarPopup .menuPopupTopLeftInner {
   width: 5px;
   height: 5px;
   overflow: hidden;
}

* html .gwt-MenuBarPopup .menuPopupTopRightInner {
   width: 8px;
   height: 5px;
   overflow: hidden;
}

* html .gwt-MenuBarPopup .menuPopupBottomLeftInner {
   width: 5px;
   height: 8px;
   overflow: hidden;
}

* html .gwt-MenuBarPopup .menuPopupBottomRightInner {
   width: 8px;
   height: 8px;
   overflow: hidden;
}

以下是修改後的 HTML 宿主檔案war/HelloWorld.html的內容。

<html>
   <head>
      <title>Hello World</title>
      <link rel = "stylesheet" href = "HelloWorld.css"/>
      <script language = "javascript" src = "helloworld/helloworld.nocache.js">
      </script>
   </head>

   <body>
      <h1>MenuBar Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

讓我們看看 Java 檔案src/com.tutorialspoint/HelloWorld.java的內容,它將演示 MenuBar 小部件的使用。

package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;

public class HelloWorld implements EntryPoint {
   
   private void showSelectedMenuItem(String menuItemName){
      Window.alert("Menu item: "+menuItemName+" selected");
   }
   
   public void onModuleLoad() {
	       
      // Create a menu bar
      MenuBar menu = new MenuBar();
      menu.setAutoOpen(true);
      menu.setWidth("100px");
      menu.setAnimationEnabled(true);

      // Create the file menu
      MenuBar fileMenu = new MenuBar(true);
      fileMenu.setAnimationEnabled(true);

      fileMenu.addItem("New", new Command() {
         @Override
         public void execute() {
            showSelectedMenuItem("New");
         }
      });
      
      fileMenu.addItem("Open", new Command() {
         @Override
         public void execute() {
            showSelectedMenuItem("Open");
         }
      });
      
      fileMenu.addSeparator();
      fileMenu.addItem("Exit", new Command() {
         @Override
         public void execute() {
            showSelectedMenuItem("Exit");
         }
      });

      // Create the edit menu
      MenuBar editMenu = new MenuBar(true);
      editMenu.setAnimationEnabled(true);

      editMenu.addItem("Undo", new Command() {
         @Override
         public void execute() {
            showSelectedMenuItem("Undo");
         }
      });
      
      editMenu.addItem("Redo", new Command() {
         @Override
         public void execute() {
            showSelectedMenuItem("Redo");
         }
      });	   
      
      editMenu.addItem("Cut", new Command() {
         @Override
         public void execute() {
            showSelectedMenuItem("Cut");
         }
      });	    
      
      editMenu.addItem("Copy", new Command() {
         @Override
         public void execute() {
            showSelectedMenuItem("Copy");
         }
      });
      
      editMenu.addItem("Paste", new Command() {
      @Override
         public void execute() {
            showSelectedMenuItem("Paste");
         }
      });

      menu.addItem(new MenuItem("File", fileMenu));
      menu.addSeparator();
      menu.addItem(new MenuItem("Edit", editMenu));

      //add the menu to the root panel
      RootPanel.get("gwtContainer").add(menu);
   }	
}

準備好所有更改後,讓我們像在GWT - 建立應用程式章節中那樣在開發模式下編譯並執行應用程式。如果應用程式一切正常,則會產生以下結果:

GWT MenuBar Widget

選擇選單欄中的任何值,都會彈出顯示所選值的警報訊息。

gwt_complex_widgets.htm
廣告