目录

JPopupMenu

介绍 (Introduction)

弹出菜单表示一个菜单,可以在组件内的指定位置动态弹出。

Class 声明 (Class Declaration)

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

public class JPopupMenu
   extends JComponent
      implements Accessible, MenuElement

类构造函数 (Class Constructors)

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

JPopupMenu()

构造一个没有“调用者”的JPopupMenu。

2

JPopupMenu(String label)

构造具有指定标题的JPopupMenu。

Class Methods

Sr.No. 方法和描述
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的Actions创建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的所有MenuKeyListener的数组。

21

PopupMenuListener[] getPopupMenuListeners()

返回使用addPopupMenuListener()添加到此JMenuItem的所有PopupMenuListener的数组。

22

SingleSelectionModel getSelectionModel()

返回处理单个选择的模型对象。

23

MenuElement[] getSubElements()

返回包含此菜单组件子菜单的MenuElements数组。

24

PopupMenuUI getUI()

返回呈现此组件的外观(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)

如果将鼠标事件视为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对象设置Popup窗口的大小。

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属性重置为当前外观的值。

方法继承 (Methods Inherited)

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

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

JPopupMenu示例

使用您选择的任何编辑器创建以下Java程序,例如D:/ 》 SWING 》 com 》 iowiki 》 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\iowiki\gui\SwingMenuDemo.java

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

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

验证以下输出。 (单击屏幕中间的。)

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