目录

JMenu

介绍 (Introduction)

Menu类表示从菜单栏部署的下拉菜单组件。

Class 声明 (Class Declaration)

以下是javax.swing.JMenu类的声明 -

public class JMenu
   extends JMenuItem
      implements Accessible, MenuElement

字段 (Field)

以下是java.awt.Component类的字段 -

  • protected JMenu.WinListener popupListener - 弹出窗口的窗口关闭侦听器。

类构造函数 (Class Constructors)

Sr.No. 构造函数和描述
1

JMenu()

构造一个没有文本的新JMenu。

2

JMenu(Action a)

构造一个菜单,其属性取自提供的Action。

3

JMenu(String s)

使用提供的字符串作为文本构造一个新的JMenu。

4

JMenu(String s, boolean b)

构造一个新的JMenu,其中提供的字符串作为其文本,并指定为可撕下的菜单。

Class Methods

Sr.No. 方法和描述
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的Actions创建JMenuItem。

11

protected JMenu.WinListener createWinListener(JPopupMenu p)

为弹出窗口创建一个窗口关闭侦听器。

12

void doClick(int pressTime)

以编程方式执行“单击”。

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()

返回菜单子组件的Components数组。

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属性。

方法继承 (Methods Inherited)

该类继承以下类中的方法 -

  • javax.swing.JAbstractButton
  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JMenu示例

使用您选择的任何编辑器创建以下Java程序,例如D:/ 》 SWING 》 com 》 iowiki 》 gui 》

SwingMenuDemo.java

package com.iowiki.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\iowiki\gui\SwingMenuDemo.java

如果没有错误发生,则表示编译成功。 使用以下命令运行该程序。

D:\SWING>java com.iowiki.gui.SwingMenuDemo

验证以下输出。 (单击“文件”菜单。)

SWING JMenu
↑回到顶部↑
WIKI教程 @2018