- GWT 教程
- GWT - 首页
- GWT - 概述
- GWT - 环境设置
- GWT - 应用程序
- GWT - 创建应用程序
- GWT - 部署应用程序
- GWT - 使用 CSS 样式
- GWT - 基本小部件
- GWT - 表单小部件
- GWT - 复杂小部件
- GWT - 布局面板
- GWT - 事件处理
- GWT - 自定义小部件
- GWT - UIBinder
- GWT - RPC 通信
- GWT - JUnit 集成
- GWT - 调试应用程序
- GWT - 国际化
- GWT - History 类
- GWT - 书签支持
- GWT - 日志框架
- GWT 有用资源
- GWT - 问答
- GWT - 快速指南
- GWT - 有用资源
- GWT - 讨论
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.List 返回一个列表,其中包含菜单栏中的 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.xml、HelloWorld.css、HelloWorld.html和HelloWorld.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 - 创建应用程序章节中那样在开发模式下编译并运行应用程序。如果应用程序一切正常,则会产生以下结果:
选择菜单栏中的任何值,都会弹出显示所选值的警报消息。