MenuBar
介绍 (Introduction)
MenuBar小部件代表标准菜单栏小部件。 菜单栏可以包含任意数量的菜单项,每个菜单项可以触发命令或打开级联菜单栏。
Class 声明 (Class Declaration)
以下是com.google.gwt.user.client.ui.MenuBar类的声明 -
public class MenuBar
extends Widget
implements PopupListener, HasAnimation,
HasCloseHandlers<PopupPanel>
CSS样式规则 (CSS Style Rules)
以下默认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 {}
类构造函数 (Class Constructors)
Sr.No. | 构造函数和描述 |
---|---|
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(资源)取代 |
6 | MenuBar(MenuBar.Resources resources) 创建一个空的水平菜单栏,使用指定的ClientBundle作为菜单图像。 |
Class Methods
Sr.No. | 功能名称和描述 |
---|---|
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中添加一条细线以分隔MenuItems的各个部分。 |
10 | MenuItemSeparator addSeparator(MenuItemSeparator separator) 在MenuBar中添加一条细线以分隔MenuItems的各个部分。 |
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.List getItems() protected java.util.List getItems() 返回包含菜单栏中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上添加一条细线,以分隔指定索引处的MenuItems部分。 |
21 | MenuItemSeparator insertSeparator(MenuItemSeparator separator, int beforeIndex) 在MenuBar上添加一条细线,以分隔指定索引处的MenuItems部分。 |
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上时启用或禁用自动对焦。 |
方法继承 (Methods Inherited)
该类继承以下类中的方法 -
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
java.lang.Object
MenuBar Widget示例
此示例将指导您完成在GWT中显示MenuBar Widget的使用的简单步骤。 按照以下步骤更新我们在GWT - Create Application的GWT应用程序GWT - Create Application章节 -
步 | 描述 |
---|---|
1 | 在com.包下创建一个名为HelloWorld的项目,如GWT - Create Application一章中所述。 |
2 | 修改HelloWorld.gwt.xml , HelloWorld.css , HelloWorld.html和HelloWorld.java ,如下所述。 保持其余文件不变。 |
3 | 编译并运行应用程序以验证实现的逻辑的结果。 |
以下是修改后的模块描述符src/com.
/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..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.
/HelloWorld.java ,它将演示MenuBar小部件的使用。package com..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 - 创建应用程序章节中那样在开发模式下编译和运行应用程序 。 如果您的应用程序一切正常,这将产生以下结果 -

选择菜单栏中的任何值,将弹出显示所选值的警报消息。