Swing - JPopupMenu 類



簡介

彈出式選單表示一個選單,可以在元件內的指定位置動態彈出。

類宣告

以下是javax.swing.JPopupMenu類的宣告:

public class JPopupMenu
   extends JComponent
      implements Accessible, MenuElement

類建構函式

序號 建構函式和描述
1

JPopupMenu()

構造一個沒有“呼叫者”的 JPopupMenu。

2

JPopupMenu(String label)

使用指定的標題構造一個 JPopupMenu。

類方法

序號 方法和描述
1

JMenuItem add(Action a)

將一個新的選單項附加到選單的末尾,該選單項分派指定的 Action 物件。

2

JMenuItem add(JMenuItem menuItem)

將指定的選單項附加到此選單的末尾。

3

JMenuItem add(String s)

建立一個具有指定文字的新選單項,並將其附加到此選單的末尾。

4

void addMenuKeyListener(MenuKeyListener l)

向彈出式選單新增 MenuKeyListener。

5

void addPopupMenuListener(PopupMenuListener l)

新增 PopupMenu 監聽器。

6

void addSeparator()

在選單的末尾附加一個新的分隔符。

7

protected PropertyChangeListener createActionChangeListener(JMenuItem b)

返回一個正確配置的 PropertyChangeListener,該監聽器會在 Action 發生更改時更新控制元件。

8

protected JMenuItem createActionComponent(Action a)

工廠方法,用於為新增到 JPopupMenu 的 Action 建立 JMenuItem。

9

protected void firePopupMenuCanceled()

通知 PopupMenuListeners 此彈出式選單已取消。

10

protected void firePopupMenuWillBecomeInvisible()

通知 PopupMenuListeners 此彈出式選單將變為不可見。

11

protected void firePopupMenuWillBecomeVisible()

通知 PopupMenuListeners 此彈出式選單將變為可見。

12

AccessibleContext getAccessibleContext()

獲取與此 JPopupMenu 關聯的 AccessibleContext。

13

Component getComponent()

返回此 JPopupMenu 元件。

14

Component getComponentAtIndex(int i)

已棄用。已替換為 Container.getComponent(int)

15

int getComponentIndex(Component c)

返回指定元件的索引。

16

static boolean getDefaultLightWeightPopupEnabled()

獲取 defaultLightWeightPopupEnabled 屬性,預設值為 true。

17

Component getInvoker()

返回此彈出式選單的“呼叫者”元件。

18

String getLabel()

返回彈出式選單的標籤。

19

Insets getMargin()

返回彈出式選單的邊框與其包含項之間的邊距(以畫素為單位)。

20

MenuKeyListener[]getMenuKeyListeners()

返回使用 addMenuKeyListener() 新增到此 JPopupMenu 的所有 MenuKeyListeners 的陣列。

21

PopupMenuListener[] getPopupMenuListeners()

返回使用 addPopupMenuListener() 新增到此 JMenuItem 的所有 PopupMenuListeners 的陣列。

22

SingleSelectionModel getSelectionModel()

返回處理單一選擇的模型物件。

23

MenuElement[] getSubElements()

返回一個 MenuElements 陣列,其中包含此選單元件的子選單。

24

PopupMenuUI getUI()

返回呈現此元件的 Look and Feel (L&F) 物件。

25

String getUIClassID()

返回呈現此元件的 L&F 類的名稱。

26

void insert(Action a, int index)

在給定位置插入指定 Action 物件的選單項。

27

void insert(Component component, int index)

在給定位置將指定的元件插入選單。

28

boolean isBorderPainted()

檢查是否應繪製邊框。

29

boolean isLightWeightPopupEnabled()

獲取 lightWeightPopupEnabled 屬性。

30

boolean isPopupTrigger(MouseEvent e)

如果 MouseEvent 被 JPopupMenu 當前安裝的 UI 視為彈出式觸發器,則返回 true。

31

boolean isVisible()

如果彈出式選單可見(當前正在顯示),則返回 true。

32

void menuSelectionChanged(boolean isIncluded)

當選單欄選擇更改以啟用或停用此選單時發出訊息。

33

void pack()

佈局容器,使其使用顯示其內容所需的最小空間。

34

protected void paintBorder(Graphics g)

如果 borderPainted 屬性為 true,則繪製彈出式選單的邊框。

35

protected String paramString()

返回此 JPopupMenu 的字串表示形式。

36

protected void processFocusEvent(FocusEvent evt)

透過將焦點事件分派給任何已註冊的 FocusListener 物件來處理在此元件上發生的焦點事件。

37

protected void processKeyEvent(KeyEvent evt)

處理按鍵事件,例如助記符和加速鍵。

38

void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)

處理從 MenuSelectionManager 轉發的按鍵事件,並根據需要使用 MenuSelectionManager 的 API 更改選單選擇。

39

void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)

此方法需要符合 MenuElement 介面,但未實現。

40

void remove(int pos)

從此彈出式選單中刪除指定索引處的元件。

41

void removeMenuKeyListener(MenuKeyListener l)

從此彈出式選單中刪除 MenuKeyListener。

42

void removePopupMenuListener(PopupMenuListener l)

刪除 PopupMenu 監聽器。

43

void setBorderPainted(boolean b)

設定是否應繪製邊框。

44

static void setDefaultLightWeightPopupEnabled(boolean aFlag)

設定 lightWeightPopupEnabled 屬性的預設值。

45

void setInvoker(Component invoker)

設定此彈出式選單的呼叫者,即要在其中顯示彈出式選單的元件。

46

void setLabel(String label)

設定彈出式選單的標籤。

47

void setLightWeightPopupEnabled(boolean aFlag)

設定 lightWeightPopupEnabled 屬性的值,預設值為 true。

48

void setLocation(int x, int y)

使用 x, y 座標設定彈出式選單左上角的位置。

49

void setPopupSize(Dimension d)

使用 Dimension 物件設定彈出視窗的大小。

50

void setPopupSize(int width, int height)

將彈出視窗的大小設定為指定寬度和高度。

51

void setSelected(Component sel)

設定當前選定的元件,這將導致選擇模型發生變化。

52

void setSelectionModel(SingleSelectionModel model)

設定模型物件以處理單一選擇。

53

void setUI(PopupMenuUI ui)

設定呈現此元件的 L&F 物件。

54

void setVisible(boolean b)

設定彈出式選單的可見性。

55

void show(Component invoker, int x, int y)

在元件呼叫者的座標空間中的 x,y 位置顯示彈出式選單。

56

void updateUI()

將 UI 屬性重置為當前外觀和風格的值。

繼承的方法

此類繼承自以下類的方法:

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

JPopupMenu 示例

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

SwingMenuDemo.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class SwingJpopupMenu{
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel; 

   public SwingJpopupMenu(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingJpopupMenu  swingMenuDemo = new SwingJpopupMenu();     
      swingMenuDemo.showPopupMenuDemo();
   }
   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 showPopupMenuDemo(){
      final JPopupMenu editMenu = new JPopupMenu("Edit"); 

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

      cutMenuItem.addActionListener(menuItemListener);
      copyMenuItem.addActionListener(menuItemListener);
      pasteMenuItem.addActionListener(menuItemListener);

      editMenu.add(cutMenuItem);
      editMenu.add(copyMenuItem);
      editMenu.add(pasteMenuItem);   

      mainFrame.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {            
            editMenu.show(mainFrame, e.getX(), e.getY());
         }               
      });
      mainFrame.add(editMenu); 
      mainFrame.setVisible(true);
   }
   class MenuItemListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {            
         statusLabel.setText(e.getActionCommand() + " MenuItem clicked.");
      }    
   }   
}

使用命令提示符編譯程式。轉到D:/ > SWING並鍵入以下命令。

D:\SWING>javac com\tutorialspoint\gui\SwingMenuDemo.java

如果未發生錯誤,則表示編譯成功。使用以下命令執行程式。

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

驗證以下輸出。(點選螢幕中間。)

SWING JPopupMenu
swing_menu.htm
廣告