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 的 Action 建立 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 的所有 MenuListeners 的陣列。

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 示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在 D:/ > SWING > com > tutorialspoint > gui > 中。

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

驗證以下輸出。(單擊“檔案”選單。)

SWING JMenu
swing_menu.htm
廣告