Swing - JMenu 类
简介
Menu 类表示从菜单栏部署的下拉菜单组件。
类声明
以下是 javax.swing.JMenu 类的声明 −
public class JMenu
extends JMenuItem
implements Accessible, MenuElement
字段
以下是 java.awt.Component 类的字段 −
protected JMenu.WinListener popupListener − 弹出窗口的关闭监听器。
类构造函数
| 序号 | 构造函数 & 描述 |
|---|---|
| 1 |
JMenu() 构造一个没有文本的新 JMenu。 |
| 2 |
JMenu(Action a) 构造一个菜单,其属性取自提供的 Action。 |
| 3 |
JMenu(String s) 使用提供的字符串作为其文本构造一个新的 JMenu。 |
| 4 |
JMenu(String s, boolean b) 使用提供的字符串作为其文本构造一个新的 JMenu,并指定是否为分离式菜单。 |
类方法
| 序号 | 方法 & 描述 |
|---|---|
| 1 |
JMenuItem add(Action a) 创建一个附加到指定 Action 对象的新菜单项并将其附加到此菜单的末尾。 |
| 2 |
Component add(Component c) 将组件附加到此菜单的末尾。 |
| 3 |
Component add(Component c, int index) 将指定组件添加到此容器的给定位置。 |
| 4 |
JMenuItem add(JMenuItem menuItem) 将菜单项附加到此菜单的末尾。 |
| 5 |
JMenuItem add(String s) 创建具有指定文本的新菜单项并将其附加到此菜单的末尾。 |
| 6 |
void addMenuListener(MenuListener l) 为菜单事件添加监听器。 |
| 7 |
void addSeparator() 在菜单末尾追加一个新的分隔符。 |
| 8 |
void applyComponentOrientation(ComponentOrientation o) 设置此菜单和其中包含的所有组件的 ComponentOrientation 属性。 |
| 9 |
protected PropertyChangeListener createActionChangeListener(JMenuItem b) 返回正确配置的 PropertyChangeListener,它会在 Action 发生更改时更新控件。 |
| 10 |
protected JMenuItem createActionComponent(Action a) 为添加到 JMenu 的操作创建 JMenuItem 的工厂方法。 |
| 11 |
protected JMenu.WinListener createWinListener(JPopupMenu p) 为弹出窗口创建一个关闭窗口的侦听器。 |
| 12 |
void doClick(int pressTime) 以编程方式执行"click"点击。 |
| 13 |
protected void fireMenuCanceled() 通知所有已注册对此事件类型的通知感兴趣的侦听器。 |
| 14 |
protected void fireMenuDeselected() 通知所有已注册对此事件类型的通知感兴趣的侦听器。 |
| 15 |
protected void fireMenuSelected() 通知所有已注册对此事件类型的通知感兴趣的侦听器。 |
| 16 |
AccessibleContext getAccessibleContext() 获取与此 JMenu 关联的 AccessibleContext。 |
| 17 |
Component getComponent() 返回用于绘制此 MenuElement 的 java.awt.Component。 |
| 18 |
int getDelay() 返回弹出或弹出子菜单之前建议的延迟(以毫秒为单位)。 |
| 19 |
JMenuItem getItem(int pos) 返回指定位置的 JMenuItem。 |
| 20 |
int getItemCount() 返回菜单上的项目数,包括分隔符。 |
| 21 |
Component getMenuComponent(int n) 返回位置 n 处的组件。 |
| 22 |
int getMenuComponentCount() 返回菜单上的组件数。 |
| 23 |
Component[] getMenuComponents() 返回菜单子组件的组件数组。 |
| 24 |
MenuListener[]getMenuListeners() 返回使用 addMenuListener() 添加到此 JMenu 的所有 MenuListener 的数组。 |
| 25 |
JPopupMenu getPopupMenu() 返回与此菜单关联的弹出菜单。 |
| 26 |
protected Point getPopupMenuOrigin() 计算 JMenu 的弹出菜单的原点。 |
| 27 |
MenuElement[] getSubElements() 返回包含此菜单组件的子菜单的 MenuElements 数组。 |
| 28 |
String getUIClassID() 返回呈现此组件的 L&F 类的名称。 |
| 29 |
JMenuItem insert(Action a, int pos) 在给定位置插入附加到指定 Action 对象的新菜单项。 |
| 30 |
JMenuItem insert(JMenuItem mi, int pos) 在给定位置插入指定的 JMenuitem。 |
| 31 |
void insert(String s, int pos) 在给定位置插入具有指定文本的新菜单项。 |
| 32 |
void insertSeparator(int index) 在指定位置插入分隔符。 |
| 33 |
boolean isMenuComponent(Component c) 如果指定的组件存在于子菜单层次结构中,则返回 true。 |
| 34 |
boolean isPopupMenuVisible() 如果菜单的弹出窗口可见,则返回 true。 |
| 35 |
boolean isSelected() 如果当前选择了菜单(突出显示),则返回 true。 |
| 36 |
boolean isTearOff() 如果菜单可以被撕掉,则返回 true。 |
| 37 |
boolean isTopLevelMenu() 如果菜单是"顶级菜单",即如果它是菜单栏的直接子级,则返回 true。 |
| 38 |
void menuSelectionChanged(boolean isIncluded) 当菜单栏选择更改以激活或停用此菜单时的消息。 |
| 39 |
protected String paramString() 返回此 JMenu 的字符串表示形式。 |
| 40 |
protected void processKeyEvent(KeyEvent evt) 处理按键事件,例如助记符和加速器。 |
| 41 |
void remove(Component c) 从此菜单中删除组件 c。 |
| 42 |
void remove(int pos) 从此菜单中删除指定索引处的菜单项。 |
| 43 |
void remove(JMenuItem item) 从此菜单中删除指定的菜单项。 |
| 44 |
void removeAll() 从此菜单中删除所有菜单项。 |
| 45 |
void removeMenuListener(MenuListener l) 移除菜单事件的监听器。 |
| 46 |
void setAccelerator(KeyStroke keyStroke) 没有为 JMenu 定义 setAccelerator。 |
| 47 |
void setComponentOrientation(ComponentOrientation o) 设置用于对该组件中的元素或文本进行排序的语言敏感方向。 |
| 48 |
void setDelay(int d) 设置弹出或弹出菜单的 PopupMenu 之前的建议延迟。 |
| 49 |
void setMenuLocation(int x, int y) 设置弹出组件的位置。 |
| 50 |
void setModel(ButtonModel newModel) 设置"菜单按钮"的数据模型,用户单击以打开或关闭菜单的标签。 |
| 51 |
void setPopupMenuVisible(boolean b) 设置菜单弹出窗口的可见性。 |
| 52 |
void setSelected(boolean b) 设置菜单的选择状态。 |
| 53 |
void updateUI() 使用当前外观的值重置 UI 属性。 |
继承的方法
这个类继承了以下类的方法 −
- javax.swing.JAbstractButton
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
JMenu 示例
在 D:/ > SWING > com > tutorialspoint > gui > 中使用您选择的任何编辑器创建以下 Java 程序
SwingMenuDemo.java
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
public class SwingMenuDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingMenuDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingMenuDemo swingMenuDemo = new SwingMenuDemo();
swingMenuDemo.showMenuDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showMenuDemo(){
//create a menu bar
final JMenuBar menuBar = new JMenuBar();
//create menus
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
final JMenu aboutMenu = new JMenu("About");
final JMenu linkMenu = new JMenu("Links");
//create menu items
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.setMnemonic(KeyEvent.VK_N);
newMenuItem.setActionCommand("New");
JMenuItem openMenuItem = new JMenuItem("Open");
openMenuItem.setActionCommand("Open");
JMenuItem saveMenuItem = new JMenuItem("Save");
saveMenuItem.setActionCommand("Save");
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.setActionCommand("Exit");
JMenuItem cutMenuItem = new JMenuItem("Cut");
cutMenuItem.setActionCommand("Cut");
JMenuItem copyMenuItem = new JMenuItem("Copy");
copyMenuItem.setActionCommand("Copy");
JMenuItem pasteMenuItem = new JMenuItem("Paste");
pasteMenuItem.setActionCommand("Paste");
MenuItemListener menuItemListener = new MenuItemListener();
newMenuItem.addActionListener(menuItemListener);
openMenuItem.addActionListener(menuItemListener);
saveMenuItem.addActionListener(menuItemListener);
exitMenuItem.addActionListener(menuItemListener);
cutMenuItem.addActionListener(menuItemListener);
copyMenuItem.addActionListener(menuItemListener);
pasteMenuItem.addActionListener(menuItemListener);
final JCheckBoxMenuItem showWindowMenu = new JCheckBoxMenuItem(
"Show About", true);
showWindowMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(showWindowMenu.getState()){
menuBar.add(aboutMenu);
} else {
menuBar.remove(aboutMenu);
}
}
});
final JRadioButtonMenuItem showLinksMenu = new JRadioButtonMenuItem(
"Show Links", true);
showLinksMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(menuBar.getMenu(3)!= null){
menuBar.remove(linkMenu);
mainFrame.repaint();
} else {
menuBar.add(linkMenu);
mainFrame.repaint();
}
}
});
//add menu items to menus
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(showWindowMenu);
fileMenu.addSeparator();
fileMenu.add(showLinksMenu);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
//add menu to menubar
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
menuBar.add(linkMenu);
//add menubar to the frame
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
}
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusLabel.setText(e.getActionCommand() + " JMenuItem clicked.");
}
}
}
使用命令提示符编译程序。 转到 D:/ > SWING 并键入以下命令。
D:\SWING>javac com\tutorialspoint\gui\SwingMenuDemo.java
如果没有报错,说明编译成功。 使用以下命令运行程序。
D:\SWING>java com.tutorialspoint.gui.SwingMenuDemo
Verify the following output. (Click on File Menu.)

